I. Summary
This is the theater version of the jquery series, which has nothing to do with the main line of jquery, and introduces some of the JavaScript details that you would normally overlook. For developers who want to consolidate JavaScript's theoretical knowledge and basic knowledge.
Two. Foreword
Recently interviewed some people, found that even experienced developers, for some basic theories and details are often blurred. Writing this article is because the first time I learned the following content, I found that I do have a harvest and understanding. In fact, we can easily ignore the details of JavaScript and more, this article is only the tip of the iceberg. I hope everyone can get a good through this article.
Three. JavaScript Object-oriented
JavaScript is an object-oriented language, although many books are explained, but there are many novice developers do not understand.
Creating objects
PS: Previously wrote a detailed article on the creation of objects (prototype method, factory method, etc.) but could not find, look back if you can find me to add in. The following is simply described.
In C # We use the New keyword to create objects, and we can use the New keyword in javascript:
var objectA = new Object();
But actually "new" can be omitted:
var objectA = Object();
But I recommend that you always declare an object with the New keyword to keep the grammar.
Create attributes and assign values
Attributes are not required to be declared in JavaScript and are created automatically when assigned:
objectA.name = "my name";
Access Properties
Generally we use the "." To access the properties of an object hierarchically:
alert(objectA.name);
Nested properties
The properties of an object can also be any JavaScript object:
var objectB = objectA;
objectB.other = objectA;
//此时下面三个值相当,并且改变其中任何一个值其余两个值都改变
objectA.name;
objectB.name;
objectB.other.name;
Using indexes
If there is a property name "School.college" on the objecta, then we cannot pass "." Access because the "ObjectA.school.college" statement refers to the College property of the school Property object that is looking for objecta.
In this case we need to set and access the properties through the index:
objectA["school.college"] = "BITI";
alert(objectA["school.college"]);
The following statements are equivalent:
objectA["school.college"] = "BITI";
var key = "school.college"
alert(objectA["school.college"]);
alert(objectA["school" + "." + "college"]);
alert(objectA[key]);