From JavaScript 1.2, you can create an object from the object initializer (Initializer). Alternatively, you can create a constructor and use the function and the new operator to initialize the object.
Using the object initializer
The syntax for creating objects through the object initializer is as follows:
var obj = {property_1: value_1, // property_# May is an identifier ... 2: value_2, // or a number ... // ..., // or a string
Here obj
is the name of the new object, each of which property_i
is an identifier (which can be a name, a number, or a string), and each value_i
is an expression whose value will be given to Property_i . obj
and assignment is optional; If you don't need to reference the object elsewhere, you don't need to assign it to a variable. (Note that where you accept a statement, you may want to enclose the object definition text in parentheses to avoid confusing the text with the block code)
Object initializers are expressions, and new objects are created where these expressions appear, so that even objects that are created by the exact same object initialization expression are not equal. It's like invoking new object () to create different entity entities.
The following is an attribute that creates a cup object with three properties. We notice that property design is also an object with attributes.
{color: " white ",
//
default value of properties weight:0.5
, Material: "China"
" Pink ", Mark: "Rose" ", fill: function (liquid, volumn) { //
todo Console.log ("the" + this . Material + "Cup is filled with" + volumn + "litre" + liquid + "." ); } }
In JavaScript version 1.1 and earlier, we cannot use object initializers to create objects, only the following methods are used to create objects.
To create an object using a constructor function
You can create objects in two steps:
- Define the type of the object by creating a constructor function. Capitalization is a very common and appropriate method of usage.
- Creates an object instance from new.
To define an object type, create a function for the object type to declare the name, properties, and methods of the type. For example, to create a cup object, we can use the following function:
functionCup (color, weight, material, Designcolor, markimage) { This. color =color; This. Weight =weight; This. Material =material; This. Design ={color:designcolor, mark:markimage}; This. Fill =function(liquid, volumn) {//TodoConsole.log ("the" + This. Color + "and" + This. Material + "Cup is filled with" + volumn + "litre" + liquid + "."); } }
Now you can create an object like this mycup
:
var New Cup ("White", 0.5, "China", "pink", "Rose");
It's time to createmycup 并且将指定的值赋给它的属性。因而 mycup.color 的值是字符串 "whhite", mycar.weight 的值是0.5(此处省略重量单位),依此类推。
You can new
create any number of objects by calling car
. For example:
var New Car ("Red", 0.4, "plastic", "Blue", "Girl"varnew Car ("Grown", 0.4, "glass", "green", "tree");
Using Object.create (ECMASCRIPT5 and later support)
Objects can also be Object.create
created using methods. This method is useful because it allows you to select its prototype object for the object you are creating, without having to define a constructor.
Object.create (proto[, Propertiesobject])
Proto refers to the prototype object of the object being created, and Propertiesobject is an object value that can contain several properties.
varCup ={color:"White",//default value of propertiesweight:0.5, Material:"China", Design: {color:"Pink", Mark:"Rose"}, fill:function(liquid, volumn) {//TodoConsole.log ("the" + This. Color + "and" + This. Material + "Cup is filled with" + volumn + "litre" + liquid + "."); } } //Create new Cup type called Mycup varMycup =object.create (Cup); Mycup.fill ("Water", 0.2);//Output:the White and the China Cup are filled with 0.2 litre water. varLilycup =object.create (Cup); Lilycup.color= "Red"; Lilycup.material= "Plastic"; Lilycup.fill ("Apple juice", 0.3);//output:the Red and plastic cup is filled with 0.3 litre apple juice.