page128 6.4 Detection Properties
JavaScript objects can be considered as collections of properties, like the following example
var obj={
V: "Variable",
Foo:function () {},
}
To determine whether a property exists in an object, you can use the In operator, hasOwnProperty (), and propertyisenumerable ()
Use format:
"V" in obj;
"foo" in obj;
Obj.hasownproperty ("V");
Obj.hasownproperty ("foo");
propertyIsEnumerable () is an enhanced version of hasOwnProperty () that returns true only if the enumerable of this property is detected to be true;
page130 Enumerable
For/in loops can iterate through all enumerable properties (including their own and inherited properties) in the loop body
var obj={
V: "Variable",
Foo:function () {},
}
For (p in obj) {
Console.log (P);
}
Object.create (obj); Used to create an object that inherits to obj
Setter and getter for the page132 property
var o={
$x: "I am o",
Get GetX () {
return this. $x;
},
Set SetX (x) {
this. $x =x;
}
};
When called:
Console.log (O.GETX);
o.setx= "New OBJ";
Console.log (O.SETX);
6.7 Attributes of a property
The properties of the object have four properties: (1) value (2) writable writeable (3) enumerable (4) configurable
var txt=object.getownpropertydescriptor ({x:1}, "X");
Console.log (TXT);
The above statement can be used to query the four characteristics of an object's property (its own property, which does not have access to an inherited property).
Prototype:
objects created by literal use Object.prototype as prototypes
The object created by new takes the prototype of the constructor function
Objects created by Object.create () use the first parameter as the prototype of the object being created.
Serialized object:
Str=json.stringify (o);
P=json.prase (str);
page144 Array
Creation of arrays
A. Using square brackets directly, arrays can store different types of elements
var empty=[];
var primes=[1,2,3,4,5];
var misc=[1,true,2, "a"];
B. Array direct amount can not be constant
var base=1024;
var arr=[base,base+1,base+2,base+3]
C. Arrays can contain arrays or object literals
var foo=[[1,2,3],{x:1,y:2},[{foo:function...},{foo2:function ...}]]
D. Created with the array constructor, can have a numeric parameter when called, the length of the array
var arr1=new Array ();
var arr2=new Array (10);
Arrays are special forms of objects, and using square brackets to access an array element is like using square brackets to access an object's properties
For a regular object
var o={};
O[1]= "one";
Array is the index value into a string
The difference between an array and a regular object is that when you use a non-negative integer within 2^32 as the property name, its Length property value is maintained automatically
In fact, there is no difference between an array's index and the object's property name, so the access to the array does not exist out of bounds.
Accessing an array index that does not exist is like accessing an undefined object property, returning undefined
Special behavior of Array property length
a=[1,2,3,4,5];
a.length=3;//there is only [one-time] in the array
a.length=0;//The element does not exist in the array at this time
The a.length=5;//array is equivalent to the new array (5)
Assigning a length to an array changes the contents of the array
The length property of the array can be changed to read-only in E5
a=[1,2,3];
Object.defineproperty (A, "length", {writable:false});
So that the length of the array cannot be changed.
To a defined read-only array push element, the program will error
Pop () corresponds to push ("param"), push is prefixed with "param" element at the end of the array, and pop () deletes the last element of the array and returns the deleted element
Shift () in Unshift (), Shift removes the first element of the array and moves the index of the subsequent elements forward. The unshift adds parameters.
Common Array Methods:
1.array.join ();
Convert all the elements in an array into strings and stitch together
When there are parameters, separate the elements with a parameter string, and the default is a comma when there is no argument: for example "1,2,3,4,5" "1-2-3-4-5"
2.array.reverse ();
Reverses the order of the array
3.array.sort (function);
Sort, when there are no parameters, sorted alphabetically, undefined in the last
When an alphabetical order is required, specify a comparison function to be passed into the sort ();
var a=[22,3,444,1111];
A.sort ();//alphabetical order
for (Var i=0,len=a.length;i<len;i++) Console.log (A[i]);
A.sort (A, b) {return b-a});//function returns positive, B returns a negative value before
Sort strings by ignoring the case of the alphabetical table
var str=["Ant", "Bug", "Dog", "cat";
Str.sort (function (S1,S2) {
St1=s1.tolowercase ();
St2=s2.tolowercase ();
if (ST1>ST2) return 1;
if (st1<st2) return-1;
return 0;
})
for (Var i=0,len=str.length;i<len;i++) Console.log (Str[i]);
4.array.contact () Creates and returns a new array
5.array.slice () returns a sub-array of the specified array
Array.splice () is similar to the slice function, but splice modifies the parent array
The array method defined in ECMA5:
1.forEach (function (V,i,arr) {
arr[i]=v+1;
});//callback function read v element value, I element index, arr array itself
Simulation implementation
var o = {
Foreach:function (callback) {
alert (this.length);
for (var i = 0, len = this.length; i < Len; i++) {
Callback && callback (This[i], I, this);
}
},
Get Length () {
var sum=0;
for (var n of this) {
Sum+=1;
}
return sum;
}
};
Object.defineproperty (o, "Length", {enumerable:false});
Object.defineproperty (O, "ForEach", {enumerable:false});
/* Pay special attention to the use of DefineProperty and defineproperties
Object.defineproperties (o,{
Length:{enumerable:false},
Foreach:{enumerable:false}
});
*/
O[0] = 0;
O[1] = 1;
O[2] = 2;
O.foreach (function (V,i,arr) {
arr[i]=v+1;
Console.log (arr[i]+ "callback");
});
2.map ()
Invocation Example:
a=[1,2,3];
B=a.map (function (x) {return x*x;});
Map returns an array of element values after the callback function operation
Simulation implementation
Map:function (callback) {
var temp=[];
for (var i = 0, len = this.length; i < Len; i++) {
Temp[i]=callback && Callback (This[i]);
}
return temp;
},
3.filter ()
Every ()
Some ()
Some methods of array objects
IsArray ()
11-2 Rhino Reading Notes