Object ()
- If the parameter is a value of a variety of primitive types, the object is converted to a wrapper object that corresponds to the original type value:
var obj = Object(1);obj instanceof Object // trueobj instanceof Number // truevar obj = Object(‘foo’);obj instanceof Object // trueobj instanceof String // truevar obj = Object(true);obj instanceof Object // trueobj instanceof Boolean // true
- If the argument is an object, return itself without making any changes:
//利用这一点,可以判断一个变量是否为对象function isObject(value) { return value === Object(value);}isObject([]) // trueisObject(true) // false
- For a general object, the
Object.keys()
Object.getOwnPropertyNames()
result is the same as the return. Only non-enumerable properties are involved, and there is a different result. Object.keys
method returns only an enumerable property , and Object.getOwnPropertyNames
The method also returns a property name that is not enumerable .
How do I calculate the number of object properties?
var obj = { p1: 123, p2: 456};Object.keys(obj).length // 2
static method of object (method of itself) classification
- The related methods of object property model;
- The method of controlling the state of the object;
- Prototype Chain method.
Object.prototype.toString can tell what kind of a value it is.
Object.prototype.toString.call(2) // “[object Number]”Object.prototype.toString.call(‘’) // “[object String]”Object.prototype.toString.call(true) // “[object Boolean]”Object.prototype.toString.call(undefined) // “[object Undefined]”Object.prototype.toString.call(null) // “[object Null]”Object.prototype.toString.call(Math) // “[object Math]”Object.prototype.toString.call({}) // “[object Object]”Object.prototype.toString.call([]) // “[object Array]”
Property Description Object Attributes
- There are six meta attributes, each of which has a property description object:
value
: attribute value;
writable
: Is it possible to write?
enumerable
: Is it possible to traverse?
configurable
: Can I configure this property description object?
get
: This accessor is called every time the property is read.
set
。
Note that the Object.getOwnPropertyDescriptor
method can only be used for properties of the object itself and not for inherited properties.
Once you have defined the accessor get
(or the stored-value function set
), you cannot writable
set the property to True or define the attribute at the same time, or you value
will get an error.
If the JSON-formatted output of the object is to exclude certain properties, you can enumerable
set these properties to false.
Configurable love and hate
configurable
When false,,,, value
writable
enumerable
and
configurable
Can not be modified;
configurable
False to False to writable
true to error, true to False is allowed;
- As long as
configurable
writable
there is a true, it can be changed through the Object.defineProperty
method value
;
- However
configurable
, when false, it is not possible to modify the target property directly by assigning a value value
;
- When
configurable
set to False, the property cannot be deleted;
How to Write
setter
And also
getter
It?
There are two ways to do this:
- Traditional methods of use
Object.defineProperty
to modify set
and get
:
var obj = Object.defineProperty({}, ‘p’, { get: function () { return ‘getter’; }, set: function (value) { console.log(‘setter: ‘ + value); }});obj.p // “getter”obj.p = 123 // “setter: 123”
- Simplified version, more extensive application:
var obj = { get p() { return ‘getter’; }, set p(value) { console.log(‘setter: ‘ + value); }};
How do I copy objects perfectly? (Even the attribute description object of the original object is not spared ~)
var extend = function (to, from) { for (var property in from) {//过滤掉继承的属性,因为getOwnPropertyDescriptor读不出继承属性 if (!from.hasOwnProperty(property)) continue; Object.defineProperty( to, property, Object.getOwnPropertyDescriptor(from, property) ); } return to;}extend({}, { get a(){ return 1 } })// { get a(){ return 1 } })
JavaScript learning experience