JS Object-oriented first knowledge

Source: Internet
Author: User

OOP Programming in JS

We often use the system's own objects, called system objects, such as: Array,date and so on. Variables under Objects: Properties called objects, functions below objects: methods called objects.

Create an empty object, var obj = {}; var obj = new Object ();

Factory Mode: Encapsulation function

function Createperson (name) {

1. Raw Materials
var obj = new Object ();
2. Processing
Obj.name = name;
Obj.showname = function () {
alert (this.name);
};
3. Appearances
return obj;

}

When new calls a function: This is the object that is created, and the return value of the function is directly this (implicit return)

Functions called after new: called constructors

So the factory way can be written like this:

function Createperson (name) {

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

}

JS Object-oriented first knowledge

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.