Li Zhan's nectar Model

Source: Internet
Author: User
var object = {    isA: function(aType) {        var self = this;        while (self) {            if (self == aType) {                return true;            }             self = self.Type;        };        return false;    }    };function Class(aBaseClass, aClassDefine) {    function class_() {        this.Type = aBaseClass;        for (var member in aClassDefine) {            this[member] = aClassDefine[member];        }     };    class_.prototype = aBaseClass;    return new class_()};function New(aClass, aParams) {    function new_() {        this.Type = aClass;        if (aClass.Create)             aClass.Create.apply(this, aParams);    };    new_.prototype = aClass;    return new new_();};var Person = Class(object, {    Create: function(name, age) {        this.name = name;        this.age = age;    },    SayHello: function() {        alert('Hello, I am ' + this.name + ', ' + this.age);    }});var Employee = Class(Person, {    Create: function(name, age, salary) {        Person.Create.call(this, name, age);        this.salary = salary;    },    ShowMeTheMoney: function() {        alert(this.name + ' $ ' + this.salary);    }});var BillGates = New(Person, ['Bill Gates', 53]);var SteveJobs = New(Employee, ['Steve Jobs', 53, 1234]);BillGates.SayHello();SteveJobs.SayHello();SteveJobs.ShowMeTheMoney();var LittleBill = New(BillGates.Type, ['Little Bill'], 6);LittleBill.SayHello();alert(BillGates.isA(Person));alert(BillGates.isA(Employee));alert(SteveJobs.isA(Person));alert(Person.isA(Employee));alert(Employee.isA(Person));

Class inheritance, another implementation method, is quite good, has a good idea, and is worth learning.

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.