JavaScript inheritance (top) object build

Source: Internet
Author: User
Tags inheritance

Quiz1

Is there a "class" in JavaScript?

Everything is the object

In JavaScript, in addition to basic data (Undefined, Null, Boolean, Number, String), the rest are objects (object).

In fact, objects in JavaScript are a collection of data and functionality. For example, we know:

var foo = new Function ("alert (' Hello world! ')");
Foo ();

It is visible that Foo is a function and an object. Again, for example:

function foo () {
    //do something
}
    
foo.data = 123;
foo["data2"] = "Hello";
    
alert (foo.data);
alert (FOO.DATA2);

Functions can also add properties like objects.

Construction of objects

Generally we use constructors to build objects, but without constructors we also have the means to build the objects we want:

function Creatperson (__name, __sex, __age) {return
    {
        name: __name, sex
        : __sex, age
        : __age,
        Get:function (__key) {
            alert (This[__key]);
    
}} var bob = Creatperson ("Bob", "male",);
Bob.get ("name");    Bob
bob.get ("sex");        Male
Bob.get ("Age");        18

But that's not enough, and I want the method to be shared. For example, when I create a Tom object with this function, the Get function is created again, which obviously wastes my memory.

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.