JavaScript prototype chain mode (prototype)

Source: Internet
Author: User



------------------------------the prototype chain method (prototype) inherits all the methods of the parent class, and the call method is the clone this invocation method---------------------

function Saycolor (Sprefix, Ssuffix) {
Alert (Sprefix + This.color + ssuffix)
}
var obj = new Object ();
Obj.color = "Red";
Saycolor.call (obj, "The color is", ", a ver nice color indeed.");

--------------------------------

function ClassA (scolor) {
This.color = Scolor;
This.sayhaha = function () {
alert (This.color);
}
}
function ClassC (scolor) {
Classa.call (this, scolor);//Current This is CLASSC itself
}
var classc = new CLASSC ("Black");
alert (Classc.color);
Classc.sayhaha ();


---------------------------------Apply--------------------------------------

function ClassA (scolor) {
This.color = Scolor;
This.saycolor = function () {
alert (This.color);
}
}

function ClassB (scolor,sname) {

Classa.apply (This, new Array (Scolor)); The first array, the current this is CLASSB itself
Classa.apply (this, arguments); The second type of arguments, the current this is CLASSB itself

THIS.name = SName;
This.sayname = function () {
alert (this.name);
}
}

var Class2 = new ClassB ("Blue", "Han");
alert (class2.name);
alert (Class2.color);
Class2.sayname ();
Class2.saycolor ();





---------------------------------Delete--------------------------------------

function ClassA (scolor) {
This.color = Scolor;
}

function ClassC (Scolor, SName) {
This.classa = ClassA;
This.classa (Scolor);
Delete This.classa;
}

var classc = new CLASSC ("Blue", "Han");
alert (Classc.color);


------------------------------the prototype chain method (prototype) inherits all the methods of the parent class, and the call method is the clone this invocation method---------------------

function ClassB () {}
Classb.prototype = new ClassA ();
ClassB.prototype.name = "";




The------prototype chain differs from call in that [prototype chain mode (prototype) inherits all the methods of the parent class, and the calling method can only inherit to this method of invocation]---------

Here we call the function with this
This.fndisplay ();

Here we prototype call the function
Class.prototype.fnDisplay = function () {}

Here we prototype call the function
Class.prototype.fnDisplay ()


------------------------------the prototype chain method (prototype) inherits all the methods of the parent class, and the call method is the clone this invocation method---------------------

function ClassA (scolor) {
This.color = Scolor;
This.saycolor = function () {
alert (This.color);
}
}
ClassA.prototype.saycolor1 = function () {
alert (This.color);
}
function ClassB (Scolor, SName) {
Classa.call (this, Scolor)//call method can only inherit to this call mode
THIS.name = SName;
}

var objb = new ClassB ("Blue", "Nicholas");
alert (Objb.color);
Objb.saycolor ();
Objb.saycolor1 ();


------------------------------the prototype chain method (prototype) inherits all the methods of the parent class, and the call method is the clone this invocation method---------------------


function ClassA (scolor) {
This.color = Scolor;
THIS.AAA = function () {
Alert ("AAA");
}
}
ClassA.prototype.saycolor = function () {
alert (This.color);
}
function ClassB (Scolor, SName) {
Classa.call (this, scolor);//Can only inherit the object and method defined by this and cannot inherit to prototype defined method
THIS.name = SName;
}

ClassB.prototype.sayName = function () {
alert (this.name);
}

var obja = new ClassA ("Red");
var objb = new ClassB ("Blue", "Nicholas");
Obja.saycolor ();
OBJB.AAA ();
Objb.saycolor ();//cannot be called because it is a prototype and cannot be inherited.
Objb.sayname ();


------------------------------the prototype chain method (prototype) inherits all the methods of the parent class, and the call method is the clone this invocation method---------------------

function ClassA (scolor) {
This.color = Scolor;
}
ClassA.prototype.saycolor = function () {
alert (This.color);
}
function ClassB (Scolor, SName) {
THIS.name = SName;
}
Classb.prototype = new ClassA ();//can inherit all defined objects and methods
ClassB.prototype.color = "Yellow";

ClassB.prototype.sayName = function () {
alert (this.name);
}

var obja = new ClassA ("Red");
var objb = new ClassB ("Blue", "Nicholas");
Obja.saycolor ();
Objb.saycolor ();//cannot be called because it is a prototype and cannot be inherited.
Objb.sayname ();


------------------------------the prototype chain method (prototype) inherits all the methods of the parent class, and the call method is the clone this invocation method---------------------


function ClassA (scolor) {
This.color = Scolor;
THIS.AAA = function () {
Alert ("AAA");
}
}
ClassA.prototype.saycolor = function () {//This method is late-bound.
alert (This.color);
}
function ClassB (Scolor, SName) {
Classa.call (this, scolor)//call way, subclasses inherit methods that are not defined by prototype prototypes.
THIS.name = SName;
}
Classb.prototype = new ClassA ();//The prototype chain inherits all methods of the parent class

ClassB.prototype.sayName = function () {
alert (this.name);
}

var obja = new ClassA ("Red");
var objb = new ClassB ("Blue", "Nicholas");
Obja.saycolor ();
OBJB.AAA ();
Objb.saycolor ();
Objb.sayname ();



------------------------------the prototype chain method (prototype) inherits all the methods of the parent class, and the call method is the clone this invocation method---------------------


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 ();

Instance = new Extendclass ();
Instance.showmsg (); Show Extendclass::showmsg
Instance.baseshowmsg (); Show Baseclass::baseshowmsg
Instance.showmsg (); Show Extendclass::showmsg

EXTENDCLASS.PROTOTYPE.BASESHOWMSG = function () {
Alert ("Extendclass::baseshowmsg Henry")
}

Instance.baseshowmsg (); Show Baseclass::baseshowmsg Henry

JavaScript prototype chain mode (prototype)

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.