JS in the prototype (reprint)

Source: Internet
Author: User

This article is reproduced from the blog park.

Lim

Source: http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html

This article is based on several knowledge points:

1 prototype design pattern

Clone () can be used in. NET to implement the prototype method

The main idea of the prototype method is that there are now 1 class A, and I want to create a class B, which is a prototype and can be extended. We call the prototype of B A.

The 2 JavaScript methods can be divided into three categories:

Class A methods

B Object method

C Prototyping Method

Example:

function people (name)
{
This.name=name;
//Object Methods
This . Introduce=function () {
alert ("My name is" +this.name);
  }
}
//class method
people.run=function () {
alert ("I can Run");
}
//Prototyping method
people.prototype.introducechinese=function () {
alert ("My name is" +this.name);
}

 

//Test

var p1=new people ("windking");

P1. Introduce ();

People.run ();

P1. Introducechinese ();

3 Obj1.func.call (obj) method

It means to treat obj as Obj1, calling the Func method

OK, here's one problem to solve:

What does prototype mean?

Each object in JavaScript has the prototype property, and the prototype property of the object in JavaScript is interpreted as a reference to the object type prototype.

A.prototype = new B ();

Understanding prototype should not confuse it with inheritance. A's prototype is an example of B, and it's understandable that a will clone the methods and properties in B all over again. A can use the methods and properties of B. The emphasis here is on cloning, not inheritance. This can happen: the prototype of A is an instance of B, and the prototype of B is also an instance of a.

Let's look at an example of an experiment:

function BaseClass ()
{
this.showmsg = function ()
  {
alert ("baseclass::showmsg");
  }
}

function Extendclass ()
{
}

Extendclass.prototype = new BaseClass ();
var instance = new Extendclass ();
instance.showmsg ();//Display baseclass::showmsg

We first defined the BaseClass class, and then we want to define extentclass, but we are going to use an instance of BaseClass as a prototype, to clone the Extendclass also contains showmsg this object method.

Extendclass.prototype = new BaseClass () can be read as: Extendclass was created as an instance of BaseClass as a prototype clone.

Then there is a question, what if the Extendclass itself contains a method with the same name as the BaseClass method?

Here is the extended Experiment 2:

function BaseClass ()
{
this.showmsg = function ()
    {
alert ("baseclass::showmsg");
    }
}

function Extendclass ()
{
this.showmsg =function ()
    {
alert ("extendclass::showmsg");
    }
}

Extendclass.prototype = new BaseClass ();
var instance = new Extendclass ();

instance.showmsg ();//Display extendclass::showmsg

Experimental results show that the function will go to the function of the main body to find, if found to run, can not find the prototype to find the function. Or it can be understood that prototype does not clone a function with the same name.

Then there will be a new problem:

What if I want to use an instance of Extendclass instance call BaseClass object method ShowMsg?

The answer is that you can use call:

Extendclass.prototype = new BaseClass ();
var instance = new Extendclass ();


var baseinstance = new BaseClass ();
Baseinstance.showMsg.call (instance);//Display baseclass::showmsg

Here's Baseinstance.showMsg.call (instance); read as "invoke instance as Baseinstance, invoke its object method ShowMsg"

Well, someone here might ask, why not BaseClass.showMsg.call (instance);

This is the difference between the object method and the class method, and we want to invoke the BaseClass object method.

Finally, the following code, if understood clearly, is understood in this article:

<script type= "Text/javascript" >

function BaseClass ()
{
this.showmsg = function ()
    {
alert ("baseclass::showmsg");
    }
   
this.baseshowmsg = function ()
    {
alert ("baseclass::baseshowmsg");
    }
}
baseclass.showmsg = function ()
{
alert ("Baseclass::showmsg static");
}

function Extendclass ()
{
this.showmsg =function ()
    {
alert ("extendclass::showmsg");
    }
}
extendclass.showmsg = function ()
{
alert ("Extendclass::showmsg static")
}

Extendclass.prototype = new BaseClass ();
var instance = new Extendclass ();

instance.showmsg ();//Display extendclass::showmsg
instance.baseshowmsg ();//Display baseclass::baseshowmsg
instance.showmsg ();//Display extendclass::showmsg

BaseClass.showMsg.call (instance);//display baseclass::showmsg static

var baseinstance = new BaseClass ();
Baseinstance.showMsg.call (instance);//Display baseclass::showmsg

</script>

JS in the prototype (reprint)

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.