Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to create objects without using new in javascript

Shulou Source: shulou.com Published: 2022-05-31 11:44:10 09月20日 Update

This article mainly introduces javascript does not use new to create objects related knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that you will have something to gain after reading this javascript article on how to create objects without new, let's take a look at it.

Javascript can also create objects without using the new operator: 1, using the "var objectName= {property name 1: value 1, property name 2: value 2 descriptors.};" statement; 2, using the "prototype object, descriptors" statement.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript's method of creating objects without using new

Object direct quantity

Using direct quantities can quickly create objects, and it is also the most efficient and easiest way. The specific usage is as follows:

Var objectName = {attribute name 1: attribute value 1, attribute name 2: attribute value 2,. Attribute name n: attribute value n}

In object literals, attribute names and attribute values are separated by colons. Attribute values can be any type of data, and attribute names can be JavaScript identifiers or string expressions. Attributes are separated by commas between attributes, and the end of the last attribute does not need a comma.

Example 1

The following code uses object literals to define two objects.

Var o = {/ / object direct quantity a: 1, / define attribute b: true / / define attribute} var o1 = {/ / object direct quantity "a": 1, / define attribute "b": true / / define attribute}

Example 2

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

Var o = {/ / object direct quantity a: function () {/ / define method return 1;}}

Example 3

If the property value is an object, you can design objects with nested structures.

Var o = {/ / object direct measurement a: {/ / nested object b: 1}}

Example 4

If you do not contain any attributes, you can define an empty object.

Var o = {} / / define an empty object direct quantity

Use Object.create

Object.create is a new static method added to ECMAScript 5 to create an instance object. This method specifies the prototype and object properties of the object. The specific usage is as follows:

Object.create (prototype, descriptors)

The parameters are described as follows:

Prototype: must be a parameter, specify a prototype object, and can be null.

Descriptors: an optional parameter, a JavaScript object that contains one or more property descriptors. The property descriptor contains data properties and accessor properties, where the data properties are described below.

Value: specifies the attribute value.

Writable: defaults to false, which sets whether the attribute value is writable.

Enumerable: defaults to false and sets whether the property can be enumerated (for/in).

Configurable: defaults to false, which sets whether attributes can be modified and deleted.

The accessor feature contains two methods, which are briefly described as follows:

Set (): sets the property value.

Get (): returns the property value.

Example 1

The following example uses Object.create to define an object that inherits null and contains two enumerable properties size and shape with property values of "large" and "round", respectively.

Var newObj = Object.create (null, {size: {/ / attribute name value: "large", / / attribute value enumerable: true / / can enumerate}, shape: {/ / attribute name value: "round", / / attribute value enumerable: true / / can enumerate}}); console.log (newObj.size); / / largeconsole.log (newObj.shape) / / roundconsole.log (Object.getPrototypeOf (newObj)); / / null

Example 2

The following example uses Object.create to define an object that has the same prototype as the object directly.

Var obj = Object.create (Object.prototype, {/ / inherit Obj.prototype prototype object x: {value: undefined, / / attribute value writable: true, / / writable configurable: true, / / configurable enumerable: true / / can enumerate}}); console.log ("obj.prototype =" + Object.getPrototypeOf (obj)) The / / "obj.prototype = [object, Object]" Object.getPrototypeOf () function gets the prototype of the original object. If you want to get the property descriptor of an object, you can use the Object.getOwnPropertyDescriptor () function.

Example 3

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

Var obj = Object.create (Object.prototype, {a: {/ / data attribute a writable: true, value: "a"}, b: {/ / Visitor property 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 position console.log (obj.b); / / 20 this is the end of the article on "javascript how to create objects without using new". Thank you for reading! I believe that everyone has a certain understanding of "javascript does not use new to create objects" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.

Tags: Properties objects examples methods prototypes properties data two functions parameters knowledge between content articles types statements commas original same simple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Apple Xiaomi Microsoft