The first one is dedicated to learning a JavaScript note.

Source: Internet
Author: User
Tags array to string modulus object object hasownproperty

JavaScript can be placed anywhere in the HTML page as a scripting language, but the browser interprets the HTML in order, so the preceding script is executed first. For example, the page display initialization of JS must be placed in the head, because the initialization is required to advance (such as the page body set CSS, etc.), and if the function is executed through the event call the position is not required.
Single-line comment, with the symbol "//" before the content of the comment
Multiline comments start with "/*" and End with "*/"

JS data type
Weak type characteristics
Original type number string Boolean null undefined
Object Funtion Array Date

Stealth Conversion
= = = Type different false
The same type as nan and anything else is not equal
objects are compared by reference to compare two objects that are not equal to each other
Try type conversions and comparisons
null==undefined
Number==string Turn number
boolean==? Turn number 1==true
Object==number|string try to convert the object to the basic type new string ("hi") = = "HI";
Other false

Wrapping Object
String Object string
Number Object number
Boolen Object Boolen
Operations that use the original type, such as calling a method, adding a property, use its wrapper type as the same as creating a new object, but when finished, the
Wrapper type removal cannot get added properties

Type detection
typeof return string suitable for function object and basic type judgment
typeof function "function" typeof NaN "number"
Note typeof number "object" typeof Null "Object" (historical cause cannot be judged null)
Instanceof based on prototype chain fit object
The prototype property of the right constructor on the prototype chain of the left-manipulated object

obj instanceof Object
Left operand basic type is direct false
You want the right operand to be a function object or a function constructor instead of throwing an error
Any one of the constructors has a prototype object property that will be used as an object prototype constructed by the new constructor
Object detection between different window or IFRAME cannot use instanceof object comparison is by referencing different window under natural difference

Object.prototype.toString.apply ([]) = = = "[Object Array]"
Suitable for built-in objects and basic type numeric functions
IE678 null and undefined return [object Object]

Constructor no object has a constructor that inherits from the prototype to construct the object.

Duck type has slice length through features such as array


An expression is the ability to calculate the worth of any available program unit
Original expression
Constant Direct Quantity
Key words
Variable

Initialization expressions for arrays, objects
{X:1,y:2} var o=new Object (); O.x=1 o.y=2;
function expression
Property-Access Expression
o.x o[' x ']
Call expression
Object creation expression new object;

Operator
Number of operands
Assignment comparison calculation digit logical string Special

var val= (n/a); Each expression is computed val=3
Object.defineproperty (obj, ' x ', {
Configurable:false,
Value:1
})
Cannot delete obj.x

In operator
function Foo () {}
Foo.prototype.x=1;
New Var obj=new Foo ();
Obj.hasownproperty (' x ');
Obj._proto_.hasownproperty (' x ');

This operator

void 0 undefined

Object
property is unordered.
A string key corresponds to value
Property label writable Enumerable configurable value method Get/set
[[Proto]] [[Class]] [[Extensible]]

Creating objects
Literal (nested)

new/prototype chain
function foo () {}
foo.prototype.z=3 [[Proto]] Object.prototype re-up prototype null
var obj=new foo (); [[Proto]] Foo.prototype
Type of obj.tostring; "Function"
' Z ' in obj; True
Obj.hasownproperty (' z '); False
Assigning a value to an object does not change the value on the prototype chain without looking at the prototype chain
Delete does not affect the prototype chain

var obj=object.create ({x:1});
Obj.hasownproperty (' x '); False
var obj=object.create (NULL);
obj.tostring; Undefined

Property Read and Write
obj.x Common
obj["X"]
for (; i<=n;i++) {
Console.log (obj[' x ' +i]);
}
var p:
For (p in obj) {//The properties on the prototype chain, order indeterminate
Console.log (Obj[p]);
}

Delete Object.prototype; False
var descriptor=object.getownpropertydescriptor (Object, ' prototype ');
descriptor.configurable; False

Global variables defined by VAR cannot be deleted by local variables
Function declarations are also global internal
It's not a delete.
If you are implicitly creating ohno=1;window.onno;//1 delete Ohno;//true
Eval () can be deleted

Attribute detection
In operator prototype chain
hasOwnProperty
Propertyis whether the enumerable of the ToString non-enumerable prototype chain is mostly non-enumerable

Object.defineproperty (object, property name, object (set label {enumerable:false,vale:1000}), default writable/configurable, and so on are false
Other ways to create objects that are enumerated by default, configurable are true

car.legs! =undefined equivalent to!==undefined!==null is not strictly equal to the next Undefined==null

for (key in obj) {
if (Obj.hasownproperty (key)) {
Console.log (key);
}
}

Another method of reading and writing attributes
Getter/setter
Get Age () {return:},
Set Age (Val) {.} or Comma separated
function foo () {
Object.defineproperty (Foo.prototype, ' Z ', {get:function () {return 1;}});
}
var obj = new Foo ();
Obj.z; 1
obj.z=100;
Obj.z; 1
Because z is not in obj when the assignment was created and there is a set get method in the prototype chain, the Get method of the prototype chain is called back instead of being written to obj as normal
can only be used
Object.defineproperty (obj, ' z ', {value:100,configurable:true});
Delete obj.z; True
Obj.z; 1

Permission settings at the property level
Object.getownpropertydescriptor ({pro:true}, ' Pro '); The object string property name of the decision
Whether the writable property can modify whether the write enumerable can be traversed for a in configurable can be modified by the delete

Var person={};
Object.defineproperty (person, ' name ', {
configurable:fales;
Writable:false;
Enumerable:true;
Value: "Bosn Ma"
});
Object.defineproperty (person, ' type ', {
configurable:true;
Writable:true;
Enumerable:false;
Value: "Object"
});
Object.keys (person);//["name"]
Object.defineproperties (person,{
Title:{value: ' Fe ', enumerable:true},/ /not written property default false
Corp:{value: ' BABA ', Enumerable:true}},
Salary:{value:50000,writable:true,enumerable:true},
luck:{
Get:function () {
return math.random () >0.5? ' Good ': ' bad ';
}
},
promote;{
Set:function (level) {
This.salary*=1 + level*0.1;
}
}
});
Object.getownpropertydescriptor (person, ' Corp ');
person.promote=2;
Person.salary;//60000

Prototype label _prototype_
Class label
var tostring=object.prototype.tostring;
function GetType (o) {return Tostring.call (o). Slice (8,-1);};
Tostring.call (NULL); "[Object Null]"
GetType (NULL); "NULL"
GetType (new number (1)); "Number"
GetType (1); "Number"
Object.prototype.toString turning parameters into object re-processing
typeof new Number (1); "Object"

Extensible whether a property on an extensible object can continue to be added
var obj={x:1.y:2}
Object.extensible (obj);
Object.preventextensions (obj);
Object.isextensible (obj); False
Obj.z = 1;
Obj.z; Undefined, add new property failed
Object.getownpropertydescriptor (obj, ' x ');
Object {value:1, writable:true, Enumerable:true, configurable:true}

Object.seal (obj);
Object.getownpropertydescriptor (obj, ' x ');
Object {value:1, writable:true, Enumerable:true, Configurable:false}
object.issealed (obj); True

Object.freeze (obj);
Object.getownpropertydescriptor (obj, ' x ');
Object {value:1, Writable:false, Enumerable:true, Configurable:false}
Object.isfrozen (obj); True
The method mentioned here is for this object does not affect the prototype chain

Serialization of
Json.stringfy
Val:undefined does not appear in serialization results
obj = {val:undefined, A:nan, b:infinity, C:new Date ()};
Json.stringify (obj); "{" A ": null," B ": null," C ":" 2015-01-20T14:15:43.910Z "}"
Json.parse (' {"X": 1} ');

Try to convert an object to a value
ToString or valueof depends on the operator and the specific case
string concatenation toString
One dollar plus good character number first valueof

Arrays are worth an ordered set, each value is called an element, and no element has a numeric position number, index. JS is a weakly typed array of element objects that can contain different types

Create array
literal var arr1=[,]//undefined*2 This is not good
size from 0 to 4294967295 (2^23-1)
constructor var arr=new Array;//u Ndefined*100 new can actually omit the
array elements read and write
Arr[5]=6;
Delete arr[0] Delete array length does not change element to undefined
array element Delete dynamic without specifying size
Arr[arr.length]=1 equivalent Arr.push (1) unshif ()
Arr.length-=1 pop () shift ()
Array Iteration
for (i=0;i<n;i++) {}
No order for (I in arr) {
if (Arr.hasownproperty (i)) {
}
}
Two-dimensional array
var arr=[[0,1],[2,3],[4,5]]; traverse i,j for Loop
Sparse array
does not contain a continuous index starting at 0 The length attribute is more than the actual number of elements. Arr1=[undefined]; var arr2=new Array (1);
0 in arr1;//true has a No. 0 element undefined
0 in arr2;//false length is 1
for in traversal is key

Array method
{} Object.prototype
[] array.prototype
Slice () slice splice () glue
Join to convert array to string default comma-delimited
function Repeatstring (str,n) {
return new Array (n+1). Join (str);
}
Repeatstring ("A", 3);//"AAA"
Reverse () changes the array in reverse order the original array is also modified
sort () Sort by default alphabetically sorted original array is modified
Arr.sort (function (A, B) {
Return a-B;
});
concat () array merge original array not modified
var arr=[1,2,3];
Arr.concat ([10,11],13); [1,2,3,10,11,13]
Arr.concat ([1,[2,3]]); [1,2,3,1,[2,3]]
Slice the element negative with the left closed right open interval of the array fragment plus the length array is not modified
Splice array to return the deleted elements of the original array is modified
arr[1,2,3,4,5];
Arr.splice (A, ' a ', ' B '); Returns [2]
arr;//[1, "A", "B", 3,4,5]

ES5 ie9+
ForEach () array traversal
Arr.foreach (function (x,index,a) {
Console.log (x + ' | ' + index + ' | ' + (A===ARR));
})
Map () array mapping does not modify the original array
Arr.map (function (x) {
return x+10;
})
Filter () Array filtering does not modify the original array
Arr.filter (function (x,index) {
return index% 3 = = = 0| | x>=8;
})
Every () some () array to verify the elements in the array
Reduce () array of element 22 operation original Array not modified
var arr=[1,2,3];
Arr.reduce (function (x, y) {
return x+y;
},0); 0+1 1+2 3+3 parameter 0 no words 1+2 3+3
Reduceright ()
Aggregates an array into one result
IndexOf () does not exist-1 arr.index (1,-3); Find 1 from 3 position
LastIndexOf ()
Determines whether an array
Array.isarray ([]); True note that the Array.prototype does not directly IsArray ()
[] instanceof Array; True
({}). tostring.apply ([]) = = = ' [Object Array] '; True
[].constructor = = = Array; True

Arrays and generic objects
Same
Can all inherit
An array is an object, and an object is not necessarily an array
You can add delete properties as objects
Different
Array Auto Update length
Accessing an array by index is often significantly faster than accessing general object properties.
Array objects inherit a large number of array manipulation methods on Array.prototype

Strings and Arrays
The string is an array of classes, but it is non-rewritable writable:false
Str.chaat ();
STR[1];
Array.prototype.join.call (str, "_");

A function is a piece of JavaScript code that is defined once but can execute and invoke multiple JS functions and objects can be manipulated and passed like other objects.
The function we call JS is a function object
General function call does not return by default returns undefined
The new construct if no return or return is the base type default return is this if the returned object is the return value of the new operation

Different invocation methods
Call Foo () directly;
Object method O.method ();
Constructor new Foo ();
Call/apply/bind Func.call (o);

Create a function
function declaration
The function declaration is pre-set and can be called before the function declaration (both function declaration and variable declaration have this action)
Anonymous is not allowed to be called immediately (function declaration is pre-parsed beforehand)
The scope of the function defined is called by the function name, and function expressions and function constructs are not
function expression
(function () {})
return function () {}
Assignment-named function expression var add=function foo (A, b) {};
Named function expressions
var func=function nfe () {};
alert (FUNC===NFE);
Ie6-8 false
ie9+ "Nfe is undefined" nfe are not accessible
You can see the name of the function when debugging
Recursive invocation with function name but not commonly used

The function constructor var function= new function (' A ', ' B ', ' Console.log (a+b); '); Function.prototype not common
The variables created inside the constructor are local variables that are not taken outside the
It can get a global variable, but not the local variables defined by the outer function that wraps it.
No name of the function

This
The Global This
Browser under Window This===window
This of the general function
Function F1 () {return this}; F1 () ===window;//true,global object
"Use strict" undefined
The function as an object method this
Do not look at how a function creates a method to look at a function call
var o = {prop:37};
function Independent () {
return this.prop;
}
O.F = Independent;
Console.log (O.F ()); Logs 37

This on the object prototype chain
var o = {f:function () {return this.a + this.b;}};
var p = object.create (o);
P.A = 1;
P.B = 4;
Console.log (P.F ()); 5

Get/set method and this
function modulus () {
Return math.sqrt (this.re * this.re + this.im * this.im);
}
var o = {
Re:1,
Im:-1,
Get phase () {
Return math.atan2 (this.im, this.re);
}
};
Object.defineproperty (o, ' modulus ', {
Get:modulus, Enumerable:true, configurable:true});
Console.log (O.phase, O.modulus); logs-0.78 1.4142

This in the constructor
Call/apply method and this
Bind method with this
function f () {
return THIS.A;
}
var g = F.bind ({A: "Test"});
Console.log (g ()); Test
var o = {a:37, f:f, g:g};
Console.log (O.F (), O.G ());

function properties and arguments
Name of Foo.name function
Foo.length parameter number
Arguments.length Number of arguments
Arguments is a class array object prototype not Array.prototype

function foo (x, y, z) {
Arguments.length; 2
Arguments[0]; 1
Arguments[0] = 10; Binding relationship Strict mode does not work arguments is a copy of the incoming parameter cannot change the original parameter
X Change to 10;
ARGUMENTS[2] = 100; Non-pass parameter loses binding relationship
Z Still undefined!!!
Arguments.callee = = = Foo; True prohibit use in strict mode
}
Foo (1, 2);
Foo.length; 3
Foo.name; "Foo"

Call/apply method
function foo (x, y) {
Console.log (x, y, this);
}
Foo.call (100, 1, 2); 1, 2, number (100)
Foo.apply (True, [3, 4]); 3, 4, Boolean (True)
Foo.apply (NULL); Undefined, undefined, window
Foo.apply (undefined); Undefined, undefined, window

function foo (x, y) {
' Use strict ';
Console.log (x, y, this);
}
Foo.apply (NULL); Undefined, undefined, null
Foo.apply (undefined); Undefined, undefined, undefined

Bind and Currying
This.x = 9;
var module = {
x:81,
Getx:function () {return this.x;}
};
Module.getx (); 81
var GetX = Module.getx;
GetX (); 9
var boundgetx = getx.bind (module);
Boundgetx (); 81
Function-Ke-Physics
function Add (A, B, c) {
Return a + B + C;
}
var func = add.bind (undefined, 100); A binding 100
Func (1, 2); 103
var func2 = func.bind (undefined, 200); B Binding 200
FUNC2 (10); 310

Bind and New

The first one to dedicate yourself to a note on JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.