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.