Home > Web Front-end > JS Tutorial > body text

Can javascript create objects without using new?

青灯夜游
Release: 2023-01-06 11:18:12
Original
3348 people have browsed it

Javascript can create objects without using the new operator. Method: 1. Use the "var objectName={attribute name 1: value 1, attribute name 2: value 2,...};" statement; 2 , use the "Object.create (prototype object, descriptors)" statement.

Can javascript create objects without using new?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript does not use the new method to create an object

Object direct quantity

Use direct quantity Objects can be created quickly and are the most efficient and easiest way. The specific usage is as follows:

var objectName = {
    属性名1 : 属性值1,
    属性名2 : 属性值2,
    ...
    属性名n : 属性值n
};
Copy after login

In object literals, the attribute name and attribute value are separated by colons. The attribute value can be any type of data, and the attribute name can be a JavaScript identifier or a string. type expression. Properties are separated by commas, and no comma is required at the end of the last property.

Example 1

The following code uses object literals to define two objects.

var o = {  //对象直接量
    a : 1,  //定义属性
    b : true  //定义属性
}
var o1 = {  //对象直接量
    "a" : 1,  //定义属性
    "b" : true  //定义属性
}
Copy after login

Example 2

The attribute value can be any type of value. If the property value is a function, the property is also called a method.

var o = {  //对象直接量
    a : function () {  //定义方法
        return 1;
    }
}
Copy after login

Example 3

If the attribute value is an object, you can design a nested structure object.

var o = {  //对象直接量
    a : {  //嵌套对象
        b : 1
    }
}
Copy after login

Example 4

You can define an empty object if it does not contain any properties.

var o = {}  //定义一个空对象直接量
Copy after login

Use Object.create

Object.create is a new static method in ECMAScript 5, used to create an instance object. This method can specify the object's prototype and object properties. The specific usage is as follows:

Object.create(prototype, descriptors)
Copy after login

The parameter description is as follows:

  • prototype: required parameter, specifies the prototype object, and can be null.

  • descriptors: Optional parameter, a JavaScript object containing one or more property descriptors. Property descriptors include data properties and accessor properties, where the data properties are described below.

  • value: Specify the attribute value.

  • writable: Default is false, sets whether the attribute value is writable.

  • enumerable: Default is false, set whether the property is enumerable (for/in).

  • configurable: Default is false, sets whether attribute characteristics can be modified and attributes deleted.

The accessor attribute contains two methods, a brief description is as follows:

  • set(): Set the attribute value.

  • get(): Returns the attribute value.

Example 1

The following example uses Object.create to define an object, inherits null, and contains two enumerable properties: size and shape. , the attribute values ​​are "large" and "round" respectively.

var newObj = Object.create (null, {
    size : {  //属性名
        value : "large",  //属性值
        enumerable : true  //可以枚举
    },
    shape : {  //属性名
        value : "round",  //属性值
        enumerable : true  //可以枚举
    }
});
console.log(newObj.size);  //large
console.log(newObj.shape);  //round
console.log(Object.getPrototypeOf(newObj));  //null
Copy after login

Example 2

The following example uses Object.create to define an object with the same prototype as the object literal.

var obj = Object.create(Object.prototype, {  //继承Obj.prototype原型对象
    x : {
        value : undefined,  //属性值
        writable : true,  //可写
        configurable : true,  //可以配置
        enumerable : true  //可以枚举
    }
});
console.log("obj.prototype = " + Object.getPrototypeOf(obj));  //"obj.prototype =[object, Object]"
Object.getPrototypeOf() 函数可获取原始对象的原型。如果要获取对象的属性描述符,可以使用 Object.getOwnPropertyDescriptor() 函数。
Copy after login

Example 3

The following example defines an object that uses accessor property b to read and write data property a.

var obj = Object.create(Object.prototype, {
    a : {  //数据属性a
        writable : true,
        value : "a"
    },
    b : {  //访问器属性b
        get : function () {
            return this.a;
        },
        set : function (value) {
            this.a = value;
        },
    }
});
console.log(obj.a);  //"a"
console.log(obj.b);  //"a"
obj.b = 20;
console.log(obj.b);  //20
Copy after login

【Related recommendations: javascript learning tutorial

The above is the detailed content of Can javascript create objects without using new?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!