JavaScript Object-oriented shared member properties and methods and prototype keyword usage _javascript techniques

Source: Internet
Author: User

This article is an example of JavaScript-object-oriented shared member properties and methods and prototype keyword usage. Share to everyone for your reference. Specifically as follows:

Shared member properties and methods, using prototype keywords

Copy Code code as follows:
<script language= "javascript" type= "Text/javascript" >
function Dog () {}
Dog.prototype.shout=function () {
Alert ("Hello, puppy");
}
Dog.prototype.name= "Yellow Puppy";
var dog1 = new Dog ();
var dog2 = new Dog ();
Dog1.shout ();
Dog2.shout ();
Dog1.name= "Small white";
Window.alert (Dog1.name+dog2.name);
</script>

Attention to knowledge points:

(1) Use this. Property name or method name, or you can share them with instantiated objects, but they are stored in a different space (stack area). In this case, each object has exclusive code, and if there are many objects, it will make the efficiency lower;
(2) using prototype, it is stored in the same stack area, that is, multiple objects sharing code. When you change one of these objects, it does not affect other objects.

I hope this article will help you with your JavaScript programming.

Related Article

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.