This article describes the shared member properties and methods of object-oriented JavaScript and the usage of prototype keywords. Share it for your reference. The details are as follows:
Share member attributes and methods, use prototype keywords
Copy the 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="Xiaobai";
window.alert(dog1.name+dog2.name);
</script>
Pay attention to knowledge points:
(1) Use this. attribute name or method name, and they can also be shared with instantiated objects, but they are stored in a different space (stack area). In this case, each object has exclusive code. If there are many objects, it will make the efficiency less effective;
(2) Use prototype and store it in the same stack area, that is, multiple objects share code. When changing one of the objects, it does not affect the other objects.
I hope this article will be helpful to everyone's JavaScript programming.