Summary of several methods for creating classes/objects in JavaScript

Source: Internet
Author: User

In JS, the Create Object is not exactly the class Object that we often call to Create. The objects in JS emphasize a composite type, creating objects in JS and accessing objects are extremely flexible.

Javascript objects are a composite type that allows you to store and access them through variable names. In another way, an object is a disordered set of attributes, each item in the set is composed of names and values (does it sound like HASH tables, dictionaries, and healthy/value pairs we often hear about ?), The value type may be a built-in type (such as number or string) or an object.

I. enclosed by a pair of braces
Copy codeThe Code is as follows:
Var emptyObj = {};
Var myObj =
{
'Id': 1, // attribute names are enclosed by quotation marks, and attributes are separated by commas.
'Name': 'myname'
};
// Var m = new myObj (); // unsupported

I don't know if all objects are declared with var. Just like the above Code, it is just a simple declaration of an object, and it has only one copy, you cannot use the new operation on an object like an instantiated class object, as in the comments section of the Code above. In this way, the reuse of objects is greatly limited. Unless the object you create requires only one copy, other methods are used to create the object.

The following describes how to access the attributes and methods of an object.
Copy codeThe Code is as follows:
Var myObj =
{
'Id': 1,
'Fun ': function (){
Document. writeln (this. id + '-' + this. name); // you can use the "object. Attribute" method to access
},
'Name': 'myobj ',
'Fun1': function (){
Document. writeln (this ['id'] + '+ this ['name']); // access data in a set
}
};
MyObj. fun ();
MyObj. fun1 ();
// Result
// 1-myObj 1 + myObj

Ii. simulate a class with the function keyword

Use this in function to reference the current object and declare the attribute by assigning values to the attribute. If var is used to declare a variable, the variable is a local variable and can only be called in the class definition.
Copy codeThe Code is as follows:
Function myClass (){
This. id = 5;
This. name = 'myclass ';
This. getName = function (){
Return this. name;
}
}
Var my = new myClass ();
Alert (my. id );
Alert (my. getName ());
// Result
// 5
// Myclass

3. Create an object in the function body, declare its attributes, and then return

You can use the method of the first point to create an Object in the function body, or use new Object () to assign values to each attribute.

However, the objects created in this way do not have smart prompts in VS2008 SP1.
Copy codeThe Code is as follows:
Function myClass (){
Var obj =
{
'Id': 2,
'Name': 'myclass'
};
Return obj;
}
Function _ myClass (){
Var obj = new Object ();
Obj. id = 1;
Obj. name = '_ myclass ';
Return obj;
}
Var my = new myClass ();
Var _ my = new _ myClass ();
Alert (my. id );
Alert (my. name );
Alert (_ my. id );
Alert (_ my. name );

// Result
// 2
// Myclass
// 1
// _ Myclass

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.