JavaScript Learning experience 06

Source: Internet
Author: User
Tags object object

Object ()
    1. 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
    1. 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.keysmethod 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
    1. The related methods of object property model;
    2. The method of controlling the state of the object;
    3. 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:
    1. value: attribute value;
    2. writable: Is it possible to write?
    3. enumerable: Is it possible to traverse?
    4. configurable: Can I configure this property description object?
    5. get: This accessor is called every time the property is read.
    6. 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
    1. configurableWhen false,,,, value writable enumerable and
      configurableCan not be modified;
    2. configurableFalse to False to writable true to error, true to False is allowed;
    3. As long as configurable writable there is a true, it can be changed through the Object.defineProperty method value ;
    4. However configurable , when false, it is not possible to modify the target property directly by assigning a value value ;
    5. When configurable set to False, the property cannot be deleted;
How to Write setterAnd also getterIt?

There are two ways to do this:

    1. 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”
    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.