JavaScript object learning experience summary_basic knowledge
- WBOYOriginal
- 2016-05-16 17:20:06938browse
1. Object creation method :
(1) Created through the new operator, new is followed by a constructor name
var object = new Object();
The constructor is called directly There is usually no return value, it just initializes the object passed in by this value; but when used with new, it returns an object value as the value of the new expression
(2) Object direct quantity
Object direct A quantity consists of a list of property descriptions enclosed in curly braces, where the property descriptions are separated by commas. Each attribute description of an object literal consists of the attribute name plus a colon and the attribute value. The attribute value can be of any type or a function
var object = { a : 1, b : 'a', c : function(){} }
2. Setting and querying object attributes
(1) Through dot operator object.p
(2) Array-like method object[ 'p']
Method 2 attributes are represented by strings, which can facilitate dynamic access to object attributes
3. Enumerate object attributes
for (p in object) {
// do something
}
p is an attribute of object. This method can only enumerate user-defined attributes, but cannot enumerate some predefined attributes and methods, such as constructor
4. Accessing undefined properties of the object returns undefined;
5. Object methods
The method definition and access of objects are similar to object properties, except that object methods are functions; in methods The object that calls the method is internally referenced through this
6. The prototype object of the object
(1) Each object has a prototype object, which inherits all the properties of its prototype object; properties Inheritance only occurs when the attribute value is read;
(2) The prototype of an object is defined by the constructor that creates and initializes the object;
(3) Each function (constructor) has A prototype attribute refers to the prototype object of the function
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