理解javascript原型

2017-05-26  本文已影响0人  吴博

原型关键概念

var a = {};
//Firefox and Chrome
Object.getPrototypeOf(a);
//Firefox , Chrome and Safari
//
a.__proto__; //[Object Object]
// all browsers
a.constructor.prototype; //[Object Object];

那何为对象呢?

对象操作API

instance of 和原型有什么关系

var A = function(a){
    this. a = a;
}
var a = new A("1");
a.__proto__ == A.prototype; // true
a instanceof A; // true

一些内置方法:

function Game(){}
var game = new Game();
Game.isPrototypeOf(game); // true
game instanceof Game; // true
function Game(){
    this.name = "2048";
}
Game.prototype.age = 20;
var game = new Game();
game.hasOwnProperty('name'); // true
game.hasOwnProperty('age'); // false
上一篇 下一篇

猜你喜欢

热点阅读