JavaScript Object-Oriented basics

Source: Internet
Author: User

Recently learned JS object-oriented, in order to allow themselves to better understand, this blog as a deepening of their learning impressions of the summary (there may be a lot of shortcomings, welcome correction).

JS through the function to create the object, and JS itself is an object, then what is the object, the object contains two kinds: 1, attributes (static characteristics) 2, methods (dynamic characteristics).

To create an object by using JS:

2. Use this keyword to set properties and methods

function student () {

THIS.name//student contains the name attribute

This.sex//student contains the gender attributes

This.show=function () {

Console.log (this.name); The actions contained in the student

}

}

Call: Var zhangsan=new student ();//zhangsan to call this student function

Zhangsan.name= "Zhangsan";//Enter the name as "Zhangsan"

zhangsan.sex= "male";//Enter gender as "male"

Zhangsan.show ();//Invoke the action contained in student (the name of the student with the console output)

3. Set Object (objects)

function student (object ()) {

Var o=new object ();

O.name=name;

O.nsex=sex;

O.show=function () {

Console.log (this.name); The actions contained in the student

};

return o;

}

JavaScript Object-Oriented basics

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.