.friends); //output: John Doe, Harry, Zhao Liualert (person2.friends);//output: John Doe, Harry, Zhao LiuAnalyzing the code above, when we add a friend for Person1, we find that Person2 has also been added a friend, but this is not what we want, and this is due to the shared nature of the prototype pattern, as long as any one instance modifies the property value in the Prototype Property
JS programming is most commonly used in the definition of two object classes. Either of the following two methods can achieve the same effect! The following two methods are commonly used in daily JS programming:
1. Mixed constructor and prototype (emphasis)
The Code is as follows:
Function car (sColor, iNumbers) {// The constructor is only used to define all non-function attributes of an
getName = function () {alert (4);}; This overwrites the function expression and makes a new assignment. Here return this, that is, execute this.getname (), which is called directly here, then this points to window. Output 1.(4), after the previous step, GetName () is covered, still output 1.5~7 questionsThe main constructor is to instantiate through new.(5), New (Foo.getname) (), GetName function is executed through the constructor, output 2Here is an issue with the return value of a constructo
Declare first: Each object in JavaScript has a constructor property and a prototype attribute. Constructor points to an object's constructor, prototype a prototype object that uses the object
Notes on javaScript object-oriented and prototype, javascript object-oriented
JavaScript is a special language. ECMAScript does not have the class concept and is different from other object
JavaScript Object-Oriented Programming (6) Inheritance using prototype chain, javascript object-oriented
Inheritance is one of the object-oriented features, and the main purpose of inheritance is to reuse. Sub-classes can reuse at
The prototype attribute of the object in JavaScript, which can return the reference of the Object Type prototype. This is a pretty straightforward explanation. To understand it, you must first correctly understand the concepts of object
This article mainly introduces the Instance Object and prototype object in JavaScript. For the constructor attribute and prototype attribute, you can refer to the following statement for a friend who needs it: each object in
The prototype attribute of the object in JavaScript, which can return the reference of the Object Type prototype. This is a pretty straightforward explanation. To understand it, you must first correctly understand the concepts of object
/* Class attributes and Object Attributes */Array. Info1 = "Info1"; // adds the class attribute Info1 to the Array.Array. prototype. Info2 = "Info2"; // adds the object attribute Info2 to the Array.
Arr = [1, 2, 3];Alert (arr. Info1); // undefinedAlert (arr. Info2); // Info2
Alert (Array. Info1); // Info1
/* Class methods and
Deep understanding of javascript constructor and prototype object, and javascript Constructor
Common Object creation Modes
Create with the new KeywordThe most basic object creation method is the same as that in most other language
The factory mode and constructor mode are introduced in javaScript object-oriented design and Javascript object-oriented design, as well as their advantages and disadvantages, today, we will continue to explain the prototype mode. Each function we create has a
(); T3.sayhi ();Obj3 inherits Obj2,obj2 inheritance Obj1. When our Obj3 instance object accesses Sayhi, we go to Obj3 's instance object and look for the Sayhi method (not found), then go to the OBJ3 prototype (not found), then go to the parent OBJ2 prototype (not found), and then go to the OBJ1
Javascript is the only widely used, prototype-inherited language, so it takes time to understand the differences between the two ways of inheriting.
The first major difference is that Javascript uses a prototype chain to inherit:
function Foo () {
this.value = n;
}
Foo.prototype = {
method:function () {}
};
The source of this article: "Object-oriented JavaScript" by Stoyan StefanovSource: Original translation of red Shi JunyuCopyright Disclaimer: If you are the original author of the original text and do not want this article to be published, you can contact the author to delete it. This translation is compiled by Shi Junyu , you can use it for learning purposes, but it is forbidden to reprint .
Fifth
result of the invocation of the prototype inheritance method, and the problem is the same, not repeated here.
By the result of the O.method () call, we see that the method defined on the parent prototype will not inherit from the quilt class.
The result of the call to O.method3 () shows that the instance method defined in the subclass also cannot access the private instance members defined in the parent cl
In JavaScript, if we have an object but don't know its constructor, how do we get its prototype object?In Chrome or in the Firefox browser, we can use the object's __proto__ property to get its prototype object directly.However, t
working principle of the prototype model, you need to understand the nature of the central ECMAScript.
Understanding the Prototype
In JavaScript, whenever a new function is created, a prototype property is created for the function based on a specific set of rules. By default, although the
calling o. method (), we can see that the method defined on the parent class prototype will not inherit from the quilt class.
By calling o. method3 (), we can see that the instance methods defined in the subclass cannot access the private instance members defined in the parent class.
Finally, we can see from the results of the call to subClass. staticMethod () that static members will not be inherited.
Finally, this example does not show that multipl
We know a way to define a common object in JavaScript, such as: Let obj == 1= ' string 'function() {alert (' func '= [' x ', ' Y ']; Console.log (obj.num); // 1console.log (obj.string); // "string"Console.log (obj.func); // function () {alert (' func ')}Console.log (Obj.arr); // ["X", "Y"]Or: Let obj = { 1, ' string ', function() {alert (' func ')}, arr: [' x ', ' Y ']}Const
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.