When a Web page effects object is generated:
such as: function BB (a) {
This.a= "KKK"
}
var b=new bb ();
At this time B is the object has the BB attribute prototype to the prototype object;
The prototype object has a constructor attribute pointing to the BB function;
So alert (b.constructor==bb.prototype.constructor)//true
Here the "with" the implementation process is to see if B has this attribute to see the prototype in the value of the property, is not a simple a=b:
such as add: b.constructor= "CCC";
Implementation: Alert (B.constructor==bb.prototype.constructor)//false; Bb.prototype.constructor is still a BB function;
Look at the inheritance of Taobao's Kissy:
Copy code code as follows:
o = function (o) {
function f () {
}
F.prototype = O;
return new F ();
},
SP = S.prototype,
RP = O (sp);
R.prototype = RP;
Alert (R.prototype.constructor==sp.constructor)
Rp.constructor = R;
Alert (R.prototype.constructor==sp.constructor)
Prototype and constructor in the web effects
And each function has a default prototype property.
If this function is used in a scene that creates a custom object, we call this function a constructor. For example, here's a simple scenario:
Constructors
function person (name) {
THIS.name = name;
}
Defines the prototype of person, in which attributes can be referenced by custom objects
Person.prototype = {
Getname:function () {
return this.name;
}
}
var Zhang = new person ("Zhangsan");
Console.log (Zhang.getname ()); "Zhangsan"
As an analogy, consider the data types in JavaScript-strings, numbers, arrays (array), objects (object), dates (date), and so on. We have reason to believe that these types are implemented as constructors within JavaScript, such as:
Defines the constructor of an array as a predefined type of javascript
function Array () {
// ...
}
Initializing an instance of an array
var arr1 = new Array (1, 56, 34, 12);
However, we prefer the following syntax definition:
var arr2 = [1, 56, 34, 12];
Many of the methods (such as Concat, join, push) of an array operation should also be defined in the prototype attribute.
In fact, all of the intrinsic data types of JavaScript have read-only prototype attributes (this is understandable: because if these types of prototype properties are modified, which predefined methods disappear), we can add our own extension methods to them.
Extending a method of obtaining the minimum value to the JavaScript intrinsic type array
Array.prototype.min = function () {
var min = this[0];
for (var i = 1; i < this.length; i++) {
if (This[i] < min) {
min = this[i];
}
}
return min;
};
Call the Min method on an instance of any array
Console.log ([1, 12].min ()); 1