javaScript types and objects

javaScript (1)

basic data types javaScript

  1. Undefined;
  2. Null;
  3. Boolean;
  4. String;
  5. Number;
  6. Symbol;
  7. Object。

Note that
JavaScript code is an undefined variable instead of keywords
javaScript distinguish +0 -0 and differentiate their method is 1 / x = ± infinity (infinity)

Can not use non-integer NUMBER == === comparison or
test the accuracy of the minimum value to determine whether the same

  console.log( Math.abs(0.1 + 0.2 - 0.3) <= Number.EPSILON);

Symbol is a new type ES6 introduced, which is a collection of all non-string objects Key's
creation Symbol Symbol function using the global

    var mySymbol = Symbol("my symbol");

javaScript classes only a private attribute of the object

javaScript objects

  • Unique identifier of an object having: even if two identical objects, it is not the same object.
  • Stateful objects: an object has state, under the same object may be in different states.
  • Objects have behavior: the state of the object, probably because its behavior changes.

In JavaScript, the state and behavior unified abstract as "property"
can add properties to objects at run time in javaScript

    var o = { a: 1 };
    o.b = 2;
    console.log(o.a, o.b); //1 2

JavaScript object attribute two
first class attribute, the attribute data of
the second class properties are accessor (getter / setter) properties

Define and view properties

    var o = { a: 1 };
    Object.defineProperty(o, "b", {value: 2, writable: false, enumerable: false, configurable: true});
    //a 和 b 都是数据属性,但特征值变化了
    Object.getOwnPropertyDescriptor(o,"a"); // {value: 1, writable: true, enumerable: true, configurable: true}
    Object.getOwnPropertyDescriptor(o,"b"); // {value: 2, writable: false, enumerable: false, configurable: true}
    o.b = 3;
    console.log(o.b); // 2

JavaScript object is running when a "set property", as the attribute string or Symbol key, to access the data attribute or characteristic values eigenvalue properties value.
{writable:true,value:1,configurable:true,enumerable:true}
Is the value, in order to be able Symbol property name, which is a feature of JavaScript objects

JavaScript prototype


  • If all objects have a private field [[prototype]], it is the prototype of the object;

  • Read a property, if the object itself does not, it will continue to access the prototype object until the prototype is empty or found.


  • Object.create create a new object with the specified prototype, the prototype can be null;
  • Object.getPrototypeOf prototype of an object is obtained;
  • Object.setPrototypeOf set up a prototype of an object.
var cat = {
    say(){
        console.log("meow~");
    },
    jump(){
        console.log("jump");
    }
}

var tiger = Object.create(cat,  {
    say:{
        writable:true,
        configurable:true,
        enumerable:true,
        value:function(){
            console.log("roar!");
        }
    }
})


var anotherCat = Object.create(cat);

anotherCat.say();

var anotherTiger = Object.create(tiger);

anotherTiger.say();

function c1(){
    this.p1 = 1;
    this.p2 = function(){
        console.log(this.p1);
    }
} 
var o1 = new c1;
o1.p2();



function c2(){
}
c2.prototype.p1 = 1;
c2.prototype.p2 = function(){
    console.log(this.p1);
}

var o2 = new c2;
o2.p2();

The first method is to modify this directly in the constructor, to add this property.
The second method is to modify the constructor prototype property pointing object, which is constructed from this prototype constructor all objects.

Object classification in JavaScript

  • Host object (host Objects): JavaScript objects provided by the host environment, their behavior is completely determined by the host environment.

  • Built-in objects (Built-in Objects): objects provided by the JavaScript language.

    • Intrinsic objects (Intrinsic Objects): by the standards, and create an object instance is automatically created with the JavaScript runtime.
    • Native object (Native Objects): Objects can Array, RegExp constructor or other special syntax built by the user through the created.
    • Common Object (Ordinary Objects):, which can be inherited by the object prototype syntax {}, Object class constructor or class definitions created keyword.

Guess you like

Origin www.cnblogs.com/singworld/p/11593822.html