Objects and JavaScriptObjects in JavaScript
Objects in JavaScript
In JavaScript, there are two ways to create Objects: declarative (literal) form and constructed form.
First, the declaration is as follows:
var myObj = { key: value // ...};
The constructor is as follows:
var myObj = new Object();myObj.key = value;
The declarative object is exactly the same as the object created by the constructor, but the constructor needs to add attributes one by one. In general, we use declarative object creation.
Original Type in JavaScript
JavaScrip has six primitive types:
- String
- Number
- Boolean
- Null
- Undefined
- Object
They do not belong to the object type. It is worth noting that null is sometimes considered an object type, mainly because typeof null returns an object (a bug in this language). In fact, null belongs to the original type.
Another misunderstanding
Everything in JavaScript is an object
Obviously, this is also wrong.
There are also some object sub-types (object sub-types) of object types. function is its sub-type (called callable object in professional terms ), in JavaScript, it is regarded as the first class. Compared to a common object, a function has callable behavior, and an object type sub-type is array (Arrays ), the content of an object is more structured than that of a common object.
There are also some built-in object child types in JavaScript, which we call as built-in objects (built-in objects). There are several types:
- String
- Number
- Boolean
- Object
- Function
- Array
- Date
- RegExp
- Error
The features of these built-in objects are even the same as those of the class in Java. in fact, they are all built-in functions (built-in functions ), these built-in functions are used as constructor,
In traditional object-oriented languages, constructor is a special method of class. When we use new to instantiate a class, its constructor is called. The constructor in JavaScript is very different from this one, the constructor in JavaScript is only a common function (functions). It uses the new operator in front of the function. They are irrelevant to the class and are not used to instantiate a class, they are just a very common function, but by the new operatorControlThey are not constructor functions, but construction callof functions.
var strPrimitive = "I am a string";typeof strPrimitive; // "string"strPrimitive instanceof String; // falsevar strObject = new String( "I am a string" );typeof strObject; // "object"strObject instanceof String; // true// inspect the object sub-typeObject.prototype.toString.call( strObject ); // [object String]
StrPrimitive belongs to the original type and strObject belongs to the object type. If we want to check the length of strPrimitive and obtain its content, we need a String-type object class to complete the operation.
Fortunately, when we convert a string of the original type (coerces) to a String of the object type, these operations are automatically completed. Therefore, in general, we do not need to create object-type variables. We only need to create the corresponding original-type variables. When necessary, JavaScript will automatically convert them to the corresponding object type.
var strPrimitive = "I am a string";console.log( strPrimitive.length ); // 13console.log( strPrimitive.charAt( 3 ) ); // "m"
Null and undefined do not have corresponding Object types. The Date type does not have the corresponding original types, such as Object, Array, Function, and RegExp. Whether we use literal or constructor, they are all object types.
Error objects are rarely created. They are automatically created when an exception occurs.
In general, the content of an object is not necessarily stored inside the object. It mainly depends on the implementation of the browser. In many cases, they are not stored in the object container, the attribute names of objects are actually stored in objects. They are mainly used as references to point to their corresponding values.
var myObject = { a: 2};myObject.a; // 2myObject["a"]; // 2
Use.Operator name and usage[""]The difference is that the latter can use UTF-8/unicode-compatible attribute names, such as ["Super-Fun!"], The former cannot pass.Operator to obtain the corresponding property value.
The property names of an object are of the string type. We can use the value of the variable as the address pointing to the property value of the object:
var wantA = true;var myObject = { a: 2};var idx;if (wantA) { idx = "a";}// laterconsole.log( myObject[idx] ); // 2
Because the object's attribute names are all of the sting type, if it is not of the string type, JavaScript first converts it to the string type. We should pay attention to this point during usage:
var myObject = { };myObject[true] = "foo";myObject[3] = "bar";myObject[myObject] = "baz";myObject["true"]; // "foo"myObject["3"]; // "bar"myObject["[object Object]"]; // "baz"
In ES6, we can use the myObject [...] syntax to calculate the object attribute name:
var prefix = "foo";var myObject = { [prefix + "bar"]: "hello", [prefix + "baz"]: "world"};myObject["foobar"]; // hellomyObject["foobaz"]; // world
The attribute name of this computing object is mainly used on Symbol (new primitive data type which has an opaque unguessable value, technically a string value). Functions in the object also do not belong to the object itself, its acquisition is no different from the acquisition of properties.
For arrays, we need to note that when we add an attribute to an array, if the attribute is of the numeric type, it will be added as an array element to the array, instead of adding attributes as common to an array object:
var myArray = [ "foo", 42, "bar" ];myArray["3"] = "baz";myArray["a"]="a";myArray.length; // 4myArray[3]; // "baz"myArray.a; //"a"
In ES5, object attributes can be described through property descriptor:
var myObject = { a: 2};Object.getOwnPropertyDescriptor( myObject, "a" );// {// value: 2,// writable: true,// enumerable: true,// configurable: true// }
The attributes of an object have three features: writable, enumerable, and retriable. Their default values are shown in the preceding example. We can use Object. defineProperty (...) to add a new property for the Object, or modify the Object Property (if it is retriable ):
var myObject = {};Object.defineProperty( myObject, "a", { value: 2, writable: true, configurable: true, enumerable: true} );myObject.a; // 2
When writable = false, it is equivalent that its object does not have the setter method:
var myObject = {};Object.defineProperty( myObject, "a", { value: 2, writable: false, // not writable! configurable: true, enumerable: true} );myObject.a = 3;myObject.a; // 2
If it is in strict mode, an error is returned:
"use strict";var myObject = {};Object.defineProperty( myObject, "a", { value: 2, writable: false, // not writable! configurable: true, enumerable: true} );myObject.a = 3; // TypeError
Retriable = false is a one-way operation, irreversible:
var myObject = { a: 2};myObject.a = 3;myObject.a; // 3Object.defineProperty( myObject, "a", { value: 4, writable: true, configurable: false, // not configurable! enumerable: true} );myObject.a; // 4myObject.a = 5;myObject.a; // 5Object.defineProperty( myObject, "a", { value: 6, writable: true, configurable: true, enumerable: true} ); // TypeError
Note that when retriable is set to false, We can configure the value of writable from true to false, but not from false to true.
Resumable = false also prevents us from using the delete operator to delete this attribute.
var myObject = { a: 2};myObject.a; // 2delete myObject.a;myObject.a; // undefinedObject.defineProperty( myObject, "a", { value: 2, writable: true, configurable: false, enumerable: true} );myObject.a; // 2delete myObject.a;myObject.a; // 2
Enumerable is mainly used to control whether the for .. in loop can be displayed.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.