First, the object is Gesha
Objects are the basic data types of JavaScript. A composite object is a value: it Many value (original value or other objects) aggregated together, these values may be accessed by name. In addition to the strings, numbers, false, true, null and undefined, the values are javascript objects.
First, the object is variable (the original values are immutable), their value can be changed, how to compare two objects for equality? Even if two objects contain the same attributes and attribute values, they are not equal. Each element of the index exactly equal the two arrays is not equal. Consider the following code
the var or a = [1,2,3 all the way ]; the var b The = [1,2,3 all the way ]; the console.log (or a == b The) // false the the var 'aa = []; the var the bb = []; the console.log ( 'aa == the bb) // false the
We generally referred to as an object reference type, and the basic data types in order to distinguish javascript region, according to the terminology is called, the object is a reference value, are subject comparing the comparison reference; if and only if they refer to the same when a group object, they are equal. Consider the following codes
var aaa = []; var bbb = aaa; bbb[0] = 1; console.log(aaa) //[1] console.log(aaa===bbb) //true
How to get a copy of an object or array (but do not want to modify the copy object is to modify the copied object or array), each element must each attribute array or explicit replication object, consider the following Code
var es = ['a','b','c']; var ea = []; for(var i=0;i<a.length;i++){ ea[i] = es[i] } console.log(ea); //["a", "b", "c"] ea[0] = 'Q'; console.log(ea); //["Q", "b", "c"] console.log(es); //["a", "b", "c"]
The most common use is to create objects, settings, search, delete, detect and enumerate its properties. Attributes include the name and value, in addition to the name and value, and some associated values for each attribute, called ' "Attribute Properties"
- Writable, indicate whether you can set the value of the property.
- Enumerable, indicate whether the property is returned in circulation by for /.
- It can be configured to indicate whether you can delete or modify this property.
The term used to distinguish the following three categories of javascript object types and properties:
- Built-in objects: ECMAscript specification defines the object or class. For example, arrays, functions, date and regular expressions are built-in objects.
- Host object: javascript interpreter is embedded defined host environment.
- Custom objects: the object is created by the operation of javascript code.
- Own property: is defined directly in the object properties.
- Inherited properties: is defined in the object's prototype object attributes.
Second, create an object
You can directly target amount , the keyword new and object.create () function to create objects.
1, the amount of direct objects
Use object directly amount easiest way to create objects, see the following example:
var empty = {}; var point = {x:1,y:2};
2, create new objects by
new operator to create and initialize a new object. Keyword call back with a new function. Constructor function is referred to herein, a constructor to create a new object initialization. Consider the following example:
var O = new new Object (); // Create an empty object, and as {} var A = new new the Array (); // Create an empty array, and [] as var D = new new a Date (); // Create a Date object current time var R & lt = new new RegExp ( "JS"); // Create a RegExp object
3, Prototype
Punctuated by little, to talk about prototypes, too lazy to type here, insert a map
4、Object.create()
Object.create (), where the first parameter is the prototype of this object. The Object.create () providing a second optional parameter for the object's properties to be further described (and then add the second parameter usage later), the Object.create () is a static function, see the following codes:
var O1 = the Object.create ({x:. 1, y: 2}); // O1 inherits the properties of the x and y
You can create an ordinary empty object
var O3 = the Object.create (Object.prototype) // O3 and {} and before new Object () as
5, attribute query and set
Look at the example:
var book = { anthor:'aaa', name:'bbb', }; console.log(book.anthor) //aaa console.log(book["name"]) //bbb
Values and settings may be acquired by Properties dot (.) Or brackets ([]) operator. But there are two points to be noted, first, [] the expression inside the brackets must return a string. Second, the arithmetic operator identifier can not be keywords.
6, inheritance
To be added -
7, property access error
First, query a nonexistent property not being given, will return undefind.
Second, if the object itself does not exist, and then you try to access an object above a property that does not exist, it will certainly be an error.
Third, to set the properties will be null and undefined error
Fourth, there are some properties are read-only and can not be reassigned, there are some objects are not allowed to add properties, but people worse, these failures are not actually operating error.
To be continued ~