JS Objects and Classes

Source: Internet
Author: User
Tags object serialization

  • JS Object category

    1. Built-in objects such as array Date Function
    2. Host objects such as windows in the browser
    3. Custom Object The user creates in the code
  • The composition of the object

    1. Property

      Differentiate by inheritance

      • Own property
      • Inheriting properties defined in the Property object prototype

      By category

      • Data properties
      • Accessor properties
        var o = {    get name() {},    set name(value) {},}
    2. Three Object attributes

      • Object Prototype prototype
      • Class of Object
      • Extension tags for objects
  • Attribute Properties

    1. Data attribute Attribute value,writable,enumerable,configurable
    2. Accessor attribute get,set,enumerable,configurable
    3. Property Descriptor

      // return {value:1,writable:true,enumrable:true,configurable:true}  Object . Getownpropertydescription ({x:1 }, "x" ); //returns undefined   
    4. for non-existent and inherited properties
    5. Set property properties

       var  o = {}; object . DefineProperty (O, "x" , {Value:1 , Writable:true , Enumrable:false , Configurable:true }) / /modified to read-only  object . DefineProperty (O, "x" , { Writable:false }) o.x = 2 ; //error  //but the properties are still configurable, so you can modify the value of x  object . DefineProperty (O, "x" , {Value:2 }); 
  • Property manipulation

    1. Delete Deletes the property, but only the free attribute is deleted
    2. Detection properties

      //in操作符var o = {x:1};"x"in o;//会从自有属性和继承属性中判断o.hasOwnProperty("x");//只会检测自有属性o.propertyIsEnumerable("x");//检测自有属性,且可枚举性为true,可枚举性是属性特性之一
  • Object prototypes

    Each JS object has a prototype object (except NULL and Object.prototype)

    varnewObject();var a = {};//两种方式的原型都是Object.prototype//此处的Object并不是 java等语言中 类的概念,而是Object构造函数

    A prototype object is a unique indicator of a class

    var o = {x:1,y:1};function A {}A.prototype = o;function B {0;newinstanceof A// true 虽然new A() 和 new B()是不同的构造函数产生的,但是他们的原型都是o

    Object.create () a static function with two parameters, the first to create a prototype of the object

    var  O1 = object . Create ({x:1 , Y:2 }); //inherited attributes X, y  var  o2 = object . Create ( null ); //create an object without a prototype, without any method, including the ToString  var  O3 = object . Create ( object . prototype); //with var a = {}   
  • Constructor Property

    1. Prototype object for each function has a constructor property

      var  F = function  f   ()  {}< Span class= "Hljs-keyword" >var  p = f.prototype;
        var  c = p.constructor;c = = = F;  
    2. The
    3. Constructor is the same

       var  o = new  F (); //o inherits F.prototype  O.constructor = = = F;
       //true   
  • Extensible markup for objects

    Object.isExtensible(o);//判断对象是否可拓展Object.preventExtensions(o);//设置为不可拓展,该过程不可逆,但是给o的原型增加属性,o仍然可以继承这些属性Object.isSealed(o);Object.seal(o);//不可拓展,并设置自有属性为不可配置Object.isFrozen()Object.freeze();//不可拓展,并设置自有属性为不可配置,属性设置为只读,setter 属性不受影响
  • Object serialization is converting to a JSON string

    JSON.stringify(o);JSON.parse();
  • Classes and Types

    typeof//对于对象都返回Objectfunction classof(o) {    returnObject.prototype.toString.call(o).slice(8,-1);}//可以返回内置对象的具体类型 比如Date RegExp等

    Both of these methods have limitations, so how do you get the type of the custom object?

      1. Using instanceof
      2. Constructor property
        function typeAndValue(x) {    ifnull)return"";    switch(x.constructor) {        caseNumber:return"Number:"+x;        case Complex:return"Complex:"+x;//处理自定义类型    }}

JS Objects and Classes

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.