Properties and methods that can be accessed by all instances that can be accessed in JavaScript in JavaScript. In many cases, it is necessary to re -assign the attributes in the prototype. Novice struggle like me), the following is a simple summary of this part of knowledge.
Basic type definitions are as follows:
Copy code code as follows:
function person () {}
Person.prototype = {
constructor: Person,
name: "Person",
Age: 100,
Friends: [a "," b "],,
getName: Function () {
Return this.name;
}
};
Define the examples of two Person, modify the name property in the instance (the attribute is defined in the prototype), the test code is as follows
Copy code code as follows:
var P1 = New Person ();
var p2 = new person ();
document.write (p1.name+"<br/>"); // Person
document.write (p2.name+"<br/>"); // Person
p1.name = "p1";
document.write (p1.name+"<br/>"); // p1
document.write (p2.name+"<br/>"); // Person
document.write (p1.hasownproperty ("name")+"<br/>"); // True belongs to the object
document.write (p2.hasownproperty ("name")+"<br/>"); // false belongs to the prototype
document.write (object.keys (p1)+"<br/>"); // name
document.write (object.keys (p2)+"<br/>"); //
document.write (Object.get.GetownPropertynames (Person.prototype)+"<br/>"); // Construction, name, Age, FRIENDS, Getname
document.write (Person.prototype.name+"<br/>"); // Person
After testing, it can be found that P1.Name = "P1" does not modify the value of name, but add a name attribute to the Namepe in the instance P1. The name attribute is already an instance attribute rather than the prototype attribute. The latter object.keys (P1) can also see that there is an additional name attribute in the P1 instance without the P2. All the transmission in JS is a value transmission. This value can be a pointer to the reference type, so the equal number does not mean to modify this reference object, but to switch the original reference relationship. The following problem illustrates this problem through the code to illustrate this problem.
Copy code code as follows:
var obj = new object ();
obj.name = "obj";
function changeObj (o) {
o.Name = "Changed";
o = new object ();
o.Name = "NewObj";
}
ChangeObj (obj);
document.write (obj.name); // Changed
In the ChangEdObj method, o = new object () does not modify the value of parameter O, but cuts off the original reference relationship, so the result is not newObj but changed, but Changeded
Next test to modify the Friends attribute in prototype in the first example. This attribute is a reference type
Copy code code as follows:
p1.friends.push ("C");
document.write (p1.friends+"<br/>"); // A, B, C
document.write (p2.friends+"<br/>"); // A, B, C
p1.friends = ["x", "y", "z"];
document.write (p1.friends+"<br/>"); // x, y, z
document.write (p2.friends+"<br/>"); // A, B, C
document.write (p1.hasownproperty ("Friends")+"<br/>"); // True belongs to the object
document.write (p2.hasownproperty ("Friends")+"<br/>"); // False belongs to the prototype.
document.write (object.keys (p1)+"<br/>"); // name, friend
document.write (object.keys (p2)+"<br/>"); //
document.write (Object.get.GetownPropertynames (Person.prototype)+"<br/>"); // Construction, name, Age, FRIENDS, Getname
document.write (Person.prototype.friends+"<br/>"); // A, B, C
The test results are basically the same as the first test. When the equal number is modified, at this time, the original quotation will be cut off and created a new attribute for the instance and covers the attributes of the same name in the prototype.
Based on these two test results, it is found that the value type attributes in the prototype cannot be directly modified in the instance (of course, this value type should not be defined in the prototype.