Object Oriented - Function

Closures and inherited
self-running functions are also called self-invoking functions, which are executed immediately after the function is declared.
An anonymous function is a function without a defined function name.
Scenario: A function that executes only once.
Features: Self-running anonymous functions can effectively ensure that JavaScript is written on the page without causing pollution of global variables.
Writing:
can be written directly

~function(){console.log("Hello world");}();
+function(){console.log("Hello world");}();
-function(){console.log("Hello world");}();
!function(){console.log("Hello world");}();
delete function(){console.log("Hello world");}();
void function(){console.log("Hello world");}();

data is required ahead

console.log(1*function(){return 7};);
console.log(16/function(){return 2};);
console.log(9%function(){return 10};);

Commonly used writing method:
(function(){console.log("hello world");})();
(function(){console.log("hello world");}())
Closure:
closure, yes Refers to a function that accesses variables in the function scope outside the function.
Closures can access local variables inside a function and let them reside in memory for a long time.

function a(){
var num = 1;
return function(){
return ++num ;
}
}
var fun = a();
alert(fun());//2
alert(fun());//3

,,,,,,,,,,,,,,,,
prototype

Every object in JavaScript has a prototype property.
The interpretation of the prototype property of an object in JavaScript is: returns a reference to the prototype of the object type.
For example, we have a function called Foo, which is constructed from its prototype.

function Foo()------>prototype----->Foo.prototype

Foo.prototype----->constructor---->function Foo()

The Foo function is constructed from the prototype object of the Foo function.
prototype is a pointer to an object. The purpose of this object is to contain properties and methods shared by all instances.

All instance objects created by the same constructor will share the same prototype.
The meaning of the birth of prototype is to realize code reuse.
It is to put the properties and methods that need to be shared into an object, and this object is the prototype (object).

function Person(_name){
 this .name = _name;
 this .age = 19; // The age property is queried from the constructor content instance when using p1.age to access. If not, query the prototype prototype property. 
}

alert( Person.prototype ); // object 
Person.prototype.age = 18; // Add property to prototype object 
var p1 = new Person("Zhang San" );
alert(p1.name +", "+p1.age); // Zhang San, 18 
var p2 = new Person("Li Si" );
alert(p2.name+", "+p2.age); //李四, 18

//p2_proto_age//Query the age attribute under the prototype.
The instanceof operator is
used to test whether an object has a constructor's prototype property in its prototype chain.
The main difference between the two is:
A.isPrototypeOf(B) determines whether the A object exists in the prototype chain of the B object.
A instanceof prototype chain B judges whether B.prototype exists in the prototype chain of A.
hasOwnProperty

Whether the property is included in the instance, the property is not looked up in the prototype.

function Person(_name){
this.name = _name;
}
var p1 = new Person("a");    
alert( p1.hasOwnProperty('name') ); // true

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325007482&siteId=291194637