Home  >  Article  >  Web Front-end  >  Detailed explanation of examples of different creation methods of objects in JS

Detailed explanation of examples of different creation methods of objects in JS

怪我咯
怪我咯Original
2017-06-29 10:28:311103browse

js objects are different from general object-oriented programming languages. Objects in js are basic prototypes. Let me introduce to you the different creation methods of objects in js. It is very good. Friends who are interested should learn together.

Objects in JavaScript are different from general object-oriented programming languages ​​​​(c++, Java, etc.), and even Few people say it is an object-oriented programming language because it has no classes. JavaScript only has objects, not instances of classes. Objects in JavaScript are based on prototypes.

1.1 Period operator creation

The object in JavaScript is actually an associative array composed of attributes. The attributes are composed of names and values. , the value type can be any data type, or functions and other objects.

Create a simple object:

var foo = {};
foo.prop_1 = 'bar';
foo.prop_2 = false;
foo.prop_3 = function() {
return 'hello world'; 
}
console.log(foo.prop_3());

Assign to foo by reference, {} is the representation method of object literal, var foo={} can also be passed var foo = new Object () to explicitly create an object.

1.2 Create objects using associative arrays.

var foo = {};
foo['prop_1'] = 'bar';
foo['prop_2'] = false;
foo['prop_3'] = function() {
return 'hello world'; 
}

Using the period operator and associative array reference in JavaScript are equivalent. The advantage of using an associative array is that when we don't know the attribute name of the object, we can use variables as the index of the associative array. For example:

var some_prop = 'prop_2';
foo[some_prop] = false;

1.3 Use object initializer to create objects

Usually we use the following method to create objects when using them:

var foo = {
prop1:'bar',
prop2:false,
prop3:function(){
return 'hello world';
}
};

This method of definition is called an object derivation initializer

1.4 Create an object through the constructor.

The objects created previously are all disposable. If we want to create multiple planned objects, there are several fixed properties, methods and can be initialized. We can create complex objects through constructors:

function User(name,uri){
this.name = name;
this.uri = uri;
this.display = function() {
console.log(this.name);
}
}

Then we can use the new statement to create objects.

var someuser = new User('byvoid','http://www.byvoid.com');

Then you can access the properties and methods of this object through someuser.

The above is the detailed content of Detailed explanation of examples of different creation methods of objects in JS. For more information, please follow other related articles on the PHP Chinese website!

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