JavaScript new constructor properties and prototype (1/3)

Source: Internet
Author: User

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

Home 1 2 3 last
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.