JS static properties and Static methods

Source: Internet
Author: User

Don't say anything, directly on the code ... (Just so willful)

varAnimal =function(){};
Animal.name = "Dog";
varA =NewAnimal ();

Console.log (a.name);

Results:

It is obvious that such calls are not possible, so what if Console.log (animal.name)?

varAnimal =function(){
animal.name++;
};
Animal.name = 0;
//var a = new Animal ();

Console.log (animal.name);

A static method is a method that you can use without declaring an instance of the class

Nani? The result is this drop ..... For Mao?

What about this?

functionAnimal () {
//alert (animal.name);
This. Say =function(){
Console.log (Animal.name);
}
};
Animal.name = "Ev2le0";
varA =NewAnimal ();

A.say ();

The result is:

Nani, crying dizzy in the WC ...

Forget it or write a static method.

functionStaticclass () {};//declaration of a class
//add a static method to this class
Staticclass.staticmethod =function(){
Console.log ("This is a static method");
}

//declaring an instance method
StaticClass.prototype.instanceMethod =function(){
Console.log ("This is a instance method");
}

//try to call a static method, using 2 different ways
varA =NewStaticclass ();

A.staticmethod ();

The result is such a drop:

Try the class name again. Method Name Invocation Mode

functionStaticclass () {};//declaration of a class
//add a static method to this class
Staticclass.staticmethod =function(){
Console.log ("This is a static method");
}

//declaring an instance method
StaticClass.prototype.instanceMethod =function(){
Console.log ("This is a instance method");
}

//try to call a static method, using 2 different ways
varA =NewStaticclass ();
//A.staticmethod ();

Staticclass.staticmethod ();

The result is such a drop:

The invocation instance method can be used in the form of the class name. Method Name ()?

functionStaticclass () {};//declaration of a class
//add a static method to this class
Staticclass.staticmethod =function(){
Console.log ("This is a static method");
}

//declaring an instance method
StaticClass.prototype.instanceMethod =function(){
Console.log ("This is a instance method");
}

//try to call a static method, using 2 different ways
varA =NewStaticclass ();
//A.staticmethod ();

Staticclass.instancemethod ();

The result is such a drop:

Summarize:

1, the class name can be used for static methods. Method Name ()

2, the instance method can only be used to honestly use the object. The method name () is called in the form.

JS static properties and Static methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.