Examples of the method of JS, static methods, instance properties, static properties

An example of a static method and examples of the method:

Let's look at an example look at the JS static and instance methods in the end what is?

Static methods:

function A(){}

A.col = 'red' // Static Property

A.sayMeS=function(){

console.log("Hello World S!");

}

A.sayMeS (); // output Hello World S!

 

Examples of methods: 

function A(){

    this.Color = "yellow" // Instance Properties

}

A.prototype.age = 14; // Instance Properties

A.prototype.sayMeE=function(){

console.log("Hello World E!");

}

var a = new A ();

a.sayMeE (); // output Hello World E!

Note: we have a way to write, as follows:

A.prototype = new Animal();

or

A.prototype = {
    aa:32323,
    say: function() { console.log("My color is " + this.color); }
}

This wording will overwrite the previous A.prototype.age and A.prototype.sayMeE, so be sure to pay attention to the order, this two way to the front, and then write their own definition of

 

Second, the difference between the two:

By definition, an example method to use this function prototype object attributes to define, static methods defined directly by A.; method calls from the static methods can be called directly by A., To a reference example defined by first variable, pointing to the new object constructor definition.

Our previous post already discussed JS object constructor prototype (see the object Js in the constructor, prototype, prototype chain and inheritance ), deepen what is understood here, before we have said function is an object, the function object the prototype property can be thought of as a pointer to a method (so do each time after creating a new instance of the constructor method must be re-created again). Such like understanding, var a A is a reference, i.e. a pointer, point A can sayMeE this method, if the direct A.sayMeE () is being given, because A is not a pointer, a.sayMeS () also I will complain, because a method is not an object.

Third, talk about inheritance:

Inheritance Example:

function A(){ }

A.prototype.sayMeE=function(){
    console.log("Hello World E!");
}

function B(){ }

B.prototype = new A (); // implements inheritance

Inheritance is the pointer B point to the object A, the A form is a constructor to construct the object.

 

Source: https://www.cnblogs.com/hanguidong/p/9296697.html

An example of a static method and examples of the method:

Let's look at an example look at the JS static and instance methods in the end what is?

Static methods:

function A(){}

A.col = 'red' // Static Property

A.sayMeS=function(){

console.log("Hello World S!");

}

A.sayMeS (); // output Hello World S!

 

Examples of methods: 

function A(){

    this.Color = "yellow" // Instance Properties

}

A.prototype.age = 14; // Instance Properties

A.prototype.sayMeE=function(){

console.log("Hello World E!");

}

var a = new A ();

a.sayMeE (); // output Hello World E!

Note: we have a way to write, as follows:

A.prototype = new Animal();

or

A.prototype = {
    aa:32323,
    say: function() { console.log("My color is " + this.color); }
}

This wording will overwrite the previous A.prototype.age and A.prototype.sayMeE, so be sure to pay attention to the order, this two way to the front, and then write their own definition of

 

Second, the difference between the two:

By definition, an example method to use this function prototype object attributes to define, static methods defined directly by A.; method calls from the static methods can be called directly by A., To a reference example defined by first variable, pointing to the new object constructor definition.

Our previous post already discussed JS object constructor prototype (see the object Js in the constructor, prototype, prototype chain and inheritance ), deepen what is understood here, before we have said function is an object, the function object the prototype property can be thought of as a pointer to a method (so do each time after creating a new instance of the constructor method must be re-created again). Such like understanding, var a A is a reference, i.e. a pointer, point A can sayMeE this method, if the direct A.sayMeE () is being given, because A is not a pointer, a.sayMeS () also I will complain, because a method is not an object.

Third, talk about inheritance:

Inheritance Example:

function A(){ }

A.prototype.sayMeE=function(){
    console.log("Hello World E!");
}

function B(){ }

B.prototype = new A (); // implements inheritance

Inheritance is the pointer B point to the object A, the A form is a constructor to construct the object.

Guess you like

Origin www.cnblogs.com/mq0036/p/12044420.html