Preview function objects and closures

Create objects;

1, the amount of direct objects.

    var point = {x: 0, y: 0}; // point is an object, with different C #, which need not necessarily have a class to create objects.

  2, create new objects by

    var d = new Date (); // Create a Date object

  3, Prototype

    Object.prototype // used to get a reference to the object prototype. All objects are directly or indirectly inherits from Object.prototype, the equivalent C # System.Object ();

    Objects created by new Date () inherits from Date.prototype and Object.prototype.

  4、Object.create()

    Object.create () is a static function, you can use it to create a new object.

 

  delete () delete () method can only delete their own property, inherit property can not be deleted. delete () simply decoupled properties and host object, attribute attributes without back operation.

 There are a = {
            author: "Jiangquhai"
            number:123
        }
        delete a.author; // This line of code can also be written delete a [ "author"]
        document.write (a.author); //a.author the value is undefined, as it has been deleted 


in: detecting whether an object contains a property
There he = {
            x:1
        }  
        document.write ( "x" in o); // Returns true x is a property of o
        document.write ( "y" in o) ; // returns false y o property is not 

whether it contains certain attributes hasOwnProperty () object to be detected
 There he = {
            x : 1
        }
        document.write (o.hasOwnProperty ( "x")); // Returns true o contain a file named "x" attribute
        document.write (o.hasOwnProperty ( "y") ); // returns false o does not contain an attribute named "y" of the 

characteristic attribute

 writeable: is writable.

    enumerable: is enumerable.

    configurable: whether configuration.

 

Closure

 

function outer() { var a = '变量1' var inner = function () { console.info(a) } return inner // inner 就是一个闭包函数,因为他能够访问到outer函数的作用域 }


Closures have three characteristics:  

  1. Function nested functions.
  2. Internal functions can reference external parameters and variables.
  3. Parameters and variables will not be garbage collection mechanism.

Closure of the benefits:

  1. We want a variable long-term storage in memory.
  2. Avoid contamination of global variables.
  3. The presence of private members

Closure package disadvantages:

  1. Permanent memory, memory usage increases. Can not be recycled garbage collection mechanism;
  2. Improper use is likely to cause a memory leak.

Guess you like

Origin www.cnblogs.com/jiangquhan/p/11827074.html
Recommended