Home >Web Front-end >JS Tutorial >Overview and examples of several ways to create classes/objects in JavaScript_javascript skills

Overview and examples of several ways to create classes/objects in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:34:42974browse

In JS, creating objects (Create Object) is not exactly creating class objects as we often say. Objects in JS emphasize a composite type. Creating objects and accessing objects in JS are extremely flexible.

JS object is a composite type, which allows you to store and access through variable names. To put it another way, an object is an unordered collection of properties. Each item in the collection is composed of a name and a value. (Does it sound a lot like the HASH tables, dictionaries, and key/value pairs we often hear about?) The value type may be a built-in type (such as number, string), or an object.
1. Surrounded by a pair of braces

Copy the code The code is as follows:

var emptyObj = {};
var myObj =
{
'id': 1, //Attribute names are enclosed in quotes and attributes are separated by commas
' name': 'myName'
};
//var m = new myObj(); //Not supported

Have you noticed that objects are declared with var? Like the above code, it simply declares an object. It has only one copy. You cannot use the new operation on it like instantiating a class object, like the comment part of the above code. This greatly limits the reuse of objects. Unless the object you create only needs one copy, consider using other methods to create the object.
Let’s take a look at how to access the properties and methods of objects.
Copy code The code is as follows:

var myObj =
{
'id ': 1,
'fun': function() {
document.writeln(this.id '-' this.name);//Access
}, 'name': 'myObj',
'fun1': function() {
document.writeln(this['id'] ' ' this['name']);//Access in collection mode
}
};
myObj.fun();
myObj.fun1();
// result
// 1-myObj 1 myObj


2. Use the function keyword to simulate class
Use this in function to refer to the current object, and declare attributes by assigning values ​​to them. If a variable is declared with var, the variable is a local variable and can only be called in the class definition.

Copy code The 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 To create an object in the function body, you can use the method in the first point, or first new Object(); and then assign values ​​to each attribute. .
However, objects created in this way do not have smart prompts in VS2008 SP1.

Copy code The 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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn