The method of creating JS object and JS class is summarized _javascript skill

Source: Internet
Author: User

The code is very simple, it is not a lot of nonsense.

Copy Code code as follows:

The first way of defining
var person=new Object (); An object was created.
Person.name= "Tom"; Call the Name property with the person object, and its value is Tom
alert (person.name); Show Name property value
Person.say=function () {//Adds a say function to the person object.
Alert ("person say");
};
Person.say ();

Copy Code code as follows:

The second way of defining
var person={
Name: "Tom",
Say:function () {
Alert ("Hello person");
}
}; An object was created.
alert (person.name);
Person.say ();
person.age=10;
alert (person.age);
The class defined in JS is the use function.
var person = function (name) {//We are defining a class. It is equivalent to having a constructor with parameters.
Properties of the THIS.name =name;//class
This.say = Method of the function () {//class.
Alert ("Say good");
}
}
var p = new Person ("fox"); Define an object p for the person class
alert (p.name); Call the Name property

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.