JS language essence butterfly book
Click on the link to download e-books
This book takes the basic foundation of JS and some experience. The first look at feel very ordinary, working for some time to find this book a lot of details are important. This book is written based on ECMA3 version, it is now widely used ECMA6 version, so some knowledge needs to be updated.
E-book author URL: https://www.crockford.com/books.html
First cream
JS caused by the special nature of the language of the essence and dross coexist, so we have to the rational use of this part
The second chapter grammar
// page 18
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
}
Note
Use comments in JS best use //. Do not use / ** / form. Because the regular expression syntax error and comments conflicts may occur.
JS is in the number of 64-bit floating-point numbers, there is no concept of int. Therefore, in JS 1.0 === 1
Index: 100 = = = 1e2 = = = 1 * 10 * 10
Strings are immutable: If you use + to connect, then create a new string (plus a character not in the original basis).
'cat'.length === 3
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key);
}
}
Judging from the name attribute of an object or prototype chain members
try-catch-throw or unskilled
typeof(null) => ‘Object’
In the% JS represents taking the remainder. When both operands are positive and modulo consistent, but there is a negative number, it appeared inconsistent.
prototype
Public method functions can be placed in the object prototype (in es6 react in, the assembly directly on the class, as the component approach to achieve multiplexing effect). Separate private process method of an object can be added directly.
if (typeof Object.beget !== 'function') {
Object.create = function(o) {
var F = function() {};
F.prototype = o;
return new F();
};
}
var another = Object.create(stooge);
another.nickname = "Moe";
When updating the prototype connection does not work. When we make changes to the prototype object does not touch the object;
Property hasOwnproperty checks the object, the object does not check the properties on the prototype chain
The third chapter Object
Property trust (prototype chain)
If you try to get the property value of an object, but the object does not correspond to the attribute name, it will go step by step the prototype chain looking for the property. If you reach the end of Object.prototype not return a value of undefined.
Prototype relationship is dynamic relationship: if we add a new property to the prototype to an object, then the property on all objects created based on the prototype visible;
Reflection reflection
Check the object has a property is a very easy thing; the data type of the object can be obtained using typeof property.
There is a problem: typeof will generate a value for the attribute any prototype (e.g. construction generating function)
Solution: Let a program check and discard property function; 2 hasOwnProperty method using the property returns a unique object prototype. the method does not check the chain.
Enumeration enumeration loop through object properties
Traverse the object properties in two cases
1. The unknown property name objects: for-in traverse the object using the property of typeof filter function, using the prototype chain hasOwnProperty filter portion.
for (let name in object) {
if(typeof object[name] !== 'function') {
console.log(name + ':' + object[name]);
}
}
2. The name attributes of known function: use an array to store the function name of the attribute; i get the length of the array, the values of properties for traversing the object.
let properties = ['name', 'age', 'sex'];
for (let i = 0; i < properties.length; i++) {
console.log(properties[i] + ":" + object[properties[i]]);
}
Properties may be obtained in the correct order (no need to consider the properties of the prototype chain)
Delete Attribute delete
Deleted object's properties: if the object has a property, the property will be removed. Delete operation will not touch any objects and properties of the prototype chain; deleted object's properties, if the prototype chain as well as the property, you also can get the property (properties on the prototype chain);
Reducing the pollution of global variables: let created using a local variable in a function. Closures in the form of limited use interface exposed outwardly. The global resource loaded into a container, such a program and other programs that will reduce conflict.