Home >Web Front-end >JS Tutorial >Sharing of attribute methods of custom objects in javascript_javascript skills

Sharing of attribute methods of custom objects in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:28:571124browse

First introduce the associative array:

Copy the code The code is as follows:

var test=new Object();
test["a"]=1;
test["b"]="string";
test["c"]=false;
alert(test["a"]);


Execute the above code and display 1.
In JavaScript, methods and properties are treated as values.
Copy code The code is as follows:

<script> <br>var test={ <br>a:1, <br>b:"string", <br>c:false, <br>d:function show(){alert("OK"); <br>} <br>}; <br>var show=test.d; <br>show(); <br></script>

Execute the above code and it will show OK. First, an object test is defined, which has four attributes a, b, c, and d. The fourth one is a method, but it is still treated as a value

var show=test.d;
means assigning d to show. At this time, show is a function. Just show(); to execute this method.
Result display

Sharing of attribute methods of custom objects in javascript_javascript skills

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn