Strict mode:
The new directive after ECMA5: "use strict"
It is not a statement, but a text expression, an earlier version of JavaScript will ignore it.
Strict mode can not use an undeclared variable.
Strict mode features:
- Inside the function declaration strict mode, with local scope. Only the internal function code is executed in accordance with strict mode.
- Do not allow the use of objects that are not declared
- Objects can not be deleted
- Function can not be deleted
- Repeat function parameter names are not allowed
- Octal values do not allow text and escape character
- Allowed to write read-only property, only the properties acquired evil
- Not allowed to delete not delete attributes such as prototype
- Does not allow for the future reserved keyword
- In strict mode, the function of this is undefined
this Keywords: depending on the location of its use, it belongs referents
- Method, refer to the owner of the object (in JavaScript function generally invoked by the function name, and the method is invoked by the object function)
- The individual case, refers to the global object (object Window)
- Function, refer to a global object
- Function, strict mode, this is undefined
- Event, this refers to the generation of HTML elements receive events
- Such a method call () and apply () may be any object to this reference
< Script > // create objects var Person = { name: " Lisi " , Age: 18 is , the fullName: function () { return the this .name + " " + the this .age; } }; // display the data objects / / the method of this method refers to the owner of the object, person.fullName this () refers to the person object document.getElementById ( " Demo " ) .innerHTML = person.fullName ();
< Script > // strict mode, the function of this is undefined " use strict " ; document.getElementById ( " Demo " ) .innerHTML = myFunction (); function myFunction () { return this ; } </ Script >
<! - in HTML event, this refers to the HTML element receives this event -> < the Button onclick = "this.style.display = 'none'" > point I disappear </ the Button >
< Script > // Call this target will point to the parameters passed in the object, so the call is person2 variables. var PERSON1 = { the fullName: function () { return the this .name + " " + the this .age; } } var PERSON2 = { name: " Lisi " , Age: 2 } Alert (person1.fullName.call (PERSON2)); </ Script >
Strict mode advantages:
- It makes it easier to write JavaScript security
- Before the acceptable bad grammar into a real error
- In general JavaScript, hit the wrong variable name creates a new global variable in strict mode throws an error, can not be created.
- In general JavaScript, if not the assignment to write property, the developer will not get any error feedback.
- In strict mode, the non-writable, read-only, there is no property assignment, or assign a value to a variable or an object that does not exist, it will throw an error,