Objects
All the JS may be appreciated that an object, such as where the array , functions , objects , etc.
Closure
1. Definition & Principle
A closure that is bound to have many variables and expression of these environment variables (usually function)
Closure scope chain variable using internal functions can access external and global variables, and external function can not access the internal variable function
Accordingly, the definition of a function within the function, a function within the function to access variables inside a function, and the variable stored in memory
The following examples, where n is a global variable with respect to f2, f2 depending on f1, then a (actually f1) so running, n continued stored in memory is not cleared
After executing nAdd function, n becomes 1000
. 1 function F1 () { 2 var n-= 999 ; . 3 Nadd = function () { . 4 n-= n-+. 1 ; . 5 } . 6 function F2 () { . 7 Alert (n-); . 8 } . 9 return F2; 10 } . 11 var = A F1 (); 12 is A (); // pop 999 13 is Nadd (); 14 A (); // pop 1000
2. Uses
The use of closure, local variables inside a function to read out the function, and so local variables inside a function stored in memory
3. advantages and disadvantages
Advantages: encapsulation strong, local variables
Disadvantages: long memory for local variables, prone to memory leaks
Package
1. Definition & Principle
The object's internal data and operational details hidden, just outside a subject of special access interface
JS, the use of the package closure to achieve an effect
Function implemented method of accessing local variables nested functions within a function, called privileged methods
. 1 function F1 () { 2 var n =. 1 ; . 3 // where f2 function allows the user to access outside of the function n, a method for the privilege . 4 the this .f2 = function () { . 5 the console.log (++ n); 6 } 7 // method equivalent to the following . 8 function F2 () { . 9 the console.log (++ n-); 10 } . 11 return F2; 12 is } 13 is var a = F1 (); 14 a ();
2. shortcomings
Package takes up memory, is not conducive to inherit
Declaratively object
1. literal
Var directly declare an object, and write a property name in {}, the attribute value, the function (method)
Note that where the attribute name need quotes
1 var myinfo = { 2 'name':'qtfy', 3 'age':24, 4 'hobby':function(){ 5 console.log("I like singing"); 6 } 7 }
2. Constructor
Var configured with a statement Object object, then add properties and methods by the object names. Attribute name = attribute value
1 var myobj = new Object(); 2 myobj.name = 'qtfy'; 3 myobj.age = 24; 4 myobj.hobby = function(){ 5 console.log("I like singing"); 6 }
3. Constructor
Definition of a function and its parameter, this refers to a function in the interior of this function, the parameter passed to add attribute values
Once defined, a function to build example
1 function test(name,age,hobby){ 2 this.name = name; 3 this.age = age; 4 this.interest = function(){ 5 console.log('my hobby is '+hobby); 6 } 7 } 8 var person = new test('qtfy',24,'singing');
4. The factory pattern
Definition of a function, a configuration function declaration inside var Object object and add properties and methods to give the object through the object name. Attribute name = attribute value, and finally returns the object
It will be appreciated as a white box, to which can be input parameters, and returns a series of the same attribute name , the method object, but between objects independent of each other , there is no correlation
1 function createObject(name,age,hobby){ 2 var obj = new Object(); 3 obj.name = name; 4 obj.age = age; 5 obj.interest = function(){ 6 console.log('my hobby is '+hobby); 7 } 8 return obj; 9 } 10 var person = createObject('qtfy',24,'singing');
The prototype model
Function define an empty (null method), to add the attribute to the blank by the function prototype property function, method
Create an instance of the function by the var statement, to produce an object
1 function createObject(){} 2 createObject.prototype.name = 'qtfy'; 3 createObject.prototype.age = 24; 4 createObject.prototype.hobby = function(){ 5 console.log('I like singing'); 6 } 7 var person = new createObject();
Further, the definition of the function empty (empty method) can be used to prototype property, passing an object in the form of key-value pairs, as attribute names and values of the empty function
This method is called json data (as long as the character string, regardless of the key must be enclosed in quotes )
1 function test(){} 2 test.prototype = { 3 'name':'qtfy', 4 'age':'24', 5 'hobby':'singing' 6 }
6. Mixed Mode
A constructor to create a function, wherein the current point with this function, the passed parameter assignment attribute values
Then the prototype model, a method of adding to the function
The last instance to build a function to create an object
1 function test(name,age){ 2 this.name = name; 3 this.age = age; 4 } 5 test.prototype.hobby = function(){ 6 console.log('my hobby is singing'); 7 } 8 var person = new test('qtfy',24);
Object traversal and storage
1. traverse the object
in k statement to traverse the object by for i
Wherein i for each object attribute name / name of the method, k is the object name
Available k [i] defined access code for each attribute value / Method
2. The objects are stored in memory
Memory is divided into memory stack, heap, code segment and data segment
Save the name of the object in the stack memory, heap memory is a point in an object address
Stored in the heap memory of the object's properties (name / value) and the method name, method name pointing snippet
The method of specific code that is object code segment