JavaScript Learning notes 8 using JSON as a prototype _ basics

Source: Internet
Author: User
The code is as follows:
Copy Code code as follows:

<script type= "Text/javascript" >
var people = {
Name: "Kym",
Age:21,
Sayhello:function () {
Alert ("Hello,my name is" + THIS.name +). I am "+ this.age);
}
}
alert (people.name);
People.sayhello ();
</script>

But we can't reuse this object, how do we take this object as a prototype?

First of all, it is impossible to have a construction method on a JSON object, so let's do a simple "factory" and write a method that is specifically responsible for creating.
Copy Code code as follows:

<script type= "Text/javascript" >
var people = {
Create:function (name, age) {
THIS.name = name;
This.age = age;
},
Sayhello:function () {
Alert ("Hello,my name is" + THIS.name +). I am "+ this.age);
}
}
People.create ("Kym", 21);
People.sayhello ();
</script>

But in this way we find that we have no way to use people as a prototype, let's review: JavaScript learning notes 7--prototype chain principle of this article, we think about this process:

var p=new people (); ==>p.__proto__=people.prototype. So when we p. SayHello () will go to the People.prototype to find, the results of nothing can be found.

This problem can be resolved if you can People.prototype.sayhello=function () {}. But we know that only function can have prototype.

So let's think about the derivation of the formula before, how can we get p. What about SayHello ()? It would be nice if I could p.__proto__=people. So let's think of a way:

Since in new time, the __proto__ of an object can only be equal to the prototype of a function, we set a function X, so that p.__proto__=x.prototype, we make x.prototype=people again. The relationship is like this:
Copy Code code as follows:

<script type= "Text/javascript" >
var people = {
Create:function (name, age) {
THIS.name = name;
This.age = age;
},
Sayhello:function () {
Alert ("Hello,my name is" + THIS.name +). I am "+ this.age);
}
};

var X = function () {};
X.prototype = people;
var p = new X ();
P.create ("Kym", 21);
P.sayhello ();
</script>

This is equivalent to using X to make an intermediate variable that allows us to access the internal properties of the JSON object. But is it not so elegant? Each time we create an object, we need to write such an auxiliary function. Well, let's wrap this process up:
Copy Code code as follows:

var Factory = {
Createpeople:function (classname,name,age) {
var temp = function () {
Classname.create (name, age);
};
Temp.prototype = ClassName;
var result = new Temp ();
return result;
}
};
var people = Factory.createpeople (People, "Kym", 21);
People. SayHello ();

But there is also a drawback, is that every time I add a class, I need to register a new method to the factory, which is very troublesome, I was a long time ago to play the method: Call and apply said about the difference between call and apply, because the parameters here are not fixed, We can't enumerate them all, so we can use apply to improve this approach here:
Copy Code code as follows:

<script type= "Text/javascript" >
var people = {
Create:function (name, age) {
THIS.name = name;
This.age = age;
},
Sayhello:function () {
Alert ("Hello,my name is" + THIS.name +). I am "+ this.age);
}
};

var Factory = {
Create:function (ClassName, params) {
var temp = function () {
ClassName.Create.apply (this, params);
};
Temp.prototype = ClassName;
var result = new Temp ();
return result;
}
};
var people = Factory.create (people,["Kym", 21]);
People. SayHello ();
</script>

In this way, a complete creation class is born! Then each time we create a "class" can be done with JSON, and then the user will be unified each time to call Factory.create () on it!

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.