小七 发表于 2015-2-7 16:28

JavaScript OOP基础知识

示例代码:
var foo = {},
    bar = new Object(),
    baz = Object.create(null);
var box = Object.create({}, {
    openLid: {
      value: function(){
            return 'something';
      },
      enumerable: true
    },
    openSecret: {
      value: '',
      enumerable: false
    }
}

var car = {};
Object.defineProperty(car, 'doors', {
    writable: true/false,
    configurable: true/false,
    enumerable: true/false,
    value: 4
});

Object.defineProperties(car, {
    'wheels': {
      writable: true,
      value: 4
    },
    'doors': {
      writable: true,
      value: 4
    }
});


for(var p in car){
    car.hasOwnProperty(p);
    console.log(p);
}

Object.keys(car);//won't show un-enumerable property
Object.getOwnPropertyNames(car);    // will show enumerable property
car.propertyIsEnumerable('doors');   


var obj = {foo: 'bar'};
Object.getOwnPropertyDescriptor(obj, 'foo');    //Object {value: 'bar', writable: true, enumerable: true, configurable: true}

var a = {};
Object.getPrototypeOf(a) === Object.prototype && Object.prototype === a.__proto__;
a.valueOf();


var bombPop = {
    wrapping: 'plastic',
    flavors: ['cherry', 'lime']
}

Object.freeze(bombPop);
Object.isFrozen(bombPop);
Object.preventExtensions(bombPop);
Object.isExtensible(bombPop);
Object.seal(bombPop);
Object.isSealed(bombPop);

Object.prototype.isPrototypeOf([]);    //true
Function.prototype.isPrototypeOf(function(){});    //true

页: [1]
查看完整版本: JavaScript OOP基础知识