1. The numeric type cannot be used as an object because the JavaScript parser will place the dot (.) number resolves to float (as a floating point literal), such as 2.toString (); Causes syntax from error, Workaround:
2..toString ();//the second point is correctly recognized
2. toString ();//note the space left to the dot
(2). toString ();//2 is evalulated first
2. Native objects inherit from Object.prototype and do not have any attribute definitions.
3. The only way to remove an object property is to use the delete operator:
var obj={baz:3};
Delete Obj.baz;
4. If delete is the object key, you need to quote, because this is the keyword in JS, if not, then in the engine conforms to the ECMASCRIPT5 specification will be a syntax error.
5. Scopes and namespaces
JS does not support block-level scopes, there are only function-level scopes, and everything is defined in a global space. Each time a variable is referenced, JS will traverse the scope up until the variable is found, and will be error-free until the global space has not been found.
6. Local Variables
The only source of a local variable is a variable that is a function parameter and a variable declared with Var within a function
7.var statements and function declarations move up
Bar (), var bar = function () {};var somevalue = 42;test (); function test (data) { if (false) { goo = 1; } else {
var goo = 2; } for (var i = 0; i < i++) { var e = data[i];} }
The above code will be converted once before execution, JS will move the VAR statement and function declaration statement up to the top of the scope
var statements got moved Herevar bar, somevalue; Default to ' undefined '//The function declaration got moved up Toofunction test (data) { var goo, I, E;//missing B Lock scope moves These here if (false) { goo = 1; } else { goo = 2; } for (i = 0; i < i++) { e = data[i];} } Bar (); Fails with a TypeError since bar is still ' undefined ' somevalue = 42; Assignments is not affected by Hoistingbar = function () {};test ();
In the first part of the code, although the IF statement seems to modify the global variable goo, it is actually modifying the local variable.
8. Variable name resolution order in function
1. First look at whether the variable is declared with Var in the current scope, and if so, use it;
2. If there is a variable in the function parameter, use it;
3. If the name of the function itself is called that name, use it;
4. Skip to the outer scope, and continue to find at step 1.
9. An anonymous function is considered an expression, so in order to be called, the expression is evaluated first
(//Evaluate the function inside the parenthesesfunction () {})//And return the function object ()//Call the result of The evaluation
There are also other implementations that will not only prevent namespace contamination, but also achieve modular results.
A few other styles for directly invoking the!function () {} () +function () {} () (function () {} ());
10. Constructors (constructors)
1. Any function called by the New keyword plays the role of the constructor, and the this reference in the constructor represents the newly created object. The new object's prototype (__PROTO__) points to the prototype of the function object being called as a constructor (prototype), and the newly created object is returned by default if the call return statement is not displayed in the function.
2. If the constructor displays the call return statement, then return only one object
function Car () { return ' Ford ';} New Car (); A new object, not ' Ford ' function person () { this.somevalue = 2; return { name: ' Charles ' };} New Test (); The returned object ({name: ' Charles '}), not including somevalue
3. If the new keyword is omitted, the newly created object will not be returned, and this will refer to the global object instead of the newly created object.
function Pirate () { This.haseyepatch = true;//gets set on the global object!} var somepirate = Pirate (); Somepirate is undefined
11.arguments objects
1.arguments holds all parameters of the pass-through function, the arguments object is not an array, but has the characteristics of an array, such as having the length property, but not inheriting the Array.prototype object. This is so that arguments cannot use the array method such as: Push,pop,slice. However, we can use the for to iterate or convert it to an array to support arrays.
var arr = Array.prototype.slice.call (arguments);//convert to array, but slow conversion, not recommended for practical use
The 2.arguments object has a getter and setter method for both its own properties and the formal parameters passed to the function, so changing the parameter value also changes the property values associated with the arguments object, and conversely, changing the properties of the arguments object will also affect the parameter values.
function foo (A, B, c) { arguments[0] = 2; A 2 b = 4; ARGUMENTS[1]; 4 var d = c; D = 9; C 3}foo (1, 2, 3);
3. Arguments objects are not created until arguments is declared within a function or passed into a function as a parameter. Another exception is the use of Arguments.callee, which degrades performance.
function foo () { Arguments.callee;//do something with this function object Arguments.callee.caller;//And the C alling function Object}function Bigloop () {for (var i = 0; i < 100000; i++) { foo ();//would normally is Inli Ned ... }
12.this references
The 1.this reference is made up of five binding methods:
1). Bind as Global object: This;//the global scope
2). Bind Global Object similarly: foo ();//call a function,this refer the global scope
3). Bound object: Test.foo ()//calling a method,this refer to test
4). Binding New object: New Foo ()//calling a constructor
5). This is set to the invoked object of the associated call object when Function.prototype call or apply is explicitly invoked
function foo (A, B, c) {}var bar = {};foo.apply (bar, [1, 2, 3]); Array would expand to the Belowfoo.call (bar, 1, 2, 3); results in a = 1, b = 2, c = 3
2. Look at the following code:
Foo.method = function () { function test () { //This is set to the global object } Test ();}
A more assumed idea is that the this in test refers to Foo, which is actually wrong, but instead refers to the global object, if you want to refer to the Foo object, you can:
Foo.method = function () { var = this; function test () {//Use the self instead of the here } Test ();}
In ECMAScript 5, you can use bind to achieve the same effect:
Foo.method = function () { var test = function () { //This is now refers to Foo }.bind (this); Test ();}
3. When assigning a method to an object, it is quite similar to a simple function call, such as the following code, which will no longer reference the Someobject object
var test = someobject.methodtest;test ();
This late binding of 4.this is the reason why the prototype inherits its work:
function Foo () {}foo.prototype.method = function () {};function bar () {}bar.prototype = Foo.prototype;new bar (). method ();
13. Named function expressions
The assignment of the named function, in the Code, bar outer scope is invalid, because the function is only assigned to Foo, however, within the bar is valid, in the previous scope and namespace mentioned: function name in its local scope is valid.
var foo = function bar () { bar ();//Works}bar ();//Referenceerror
14.for in loop
When used to iterate over an object's properties, the prototype chain is traversed, and sometimes we need to filter out the properties of the function itself, and we can use Object.prototype's hasOwnProperty (in EMAScript3 or earlier).
Poisoning Object.prototypeObject.prototype.bar = 1;var foo = {moo:2};for (var i in foo) { console.log (i);//Prints Both bar and moo}//----------------------------------------------I am split line----------------------------//Still the Foo From Abovefor (var i in foo) { if (Foo.hasownproperty (i)) { console.log (i);} }
15.hasOwnProperty
Used to detect that a property is from the function itself and not the prototype chain elsewhere. If there is such a property in the object with the same name, it is necessary to extend the hasownproperty.
var foo = { hasownproperty:function () { return false; }, bar: ' Here is Dragons '};foo.hasownproperty (' Bar '); Always returns false//use another Object ' s hasownproperty and call it with ' this ' set to Foo ({}). Hasownproperty.call (f Oo, ' bar '); true//It ' s also possible to use hasOwnProperty from the object//prototype for this PurposeObject.prototype.hasOwnProp Erty.call (foo, ' Bar '); True
16.Prototype: Native type assigned to Prototype is ignored.
function Foo () {}foo.prototype = 1; No effect
From Javsscript garden harvesting