标签:
function foo created via new Function
var foo=new Function(); //equals to function foo(){} but the name is ‘‘ foo.__proto__===Function.prototype //true var ff=new foo(); //equals to new (function foo(){}) ff.__proto__==foo.prototype //true var ff2=new (new Function()); //just for test ff2.__proto__.__proto__===Object.prototype //true
Object.create polyfill
if(typeof Object.create !=‘function‘){ Object.prototype.create=function(){ //use the shared constructor to save memory var tmp=function(){}; return function(t){ //If Type (t) is not Object or Null throw a TypeError exeption. if(typeof t!=‘object‘){ throw TypeError(‘Object prototype may only be an Object or null‘); } var obj; tmp.prototype=t; obj=new tmp(); tmp.prototype=null; return obj; } } }
use Object.create and Prototype to Class Inherit
var Person=function(){ this.canWalk=true; this.canSwim=true; this.canFly=false; } Person.prototype.greet=function(){ console.log(‘greet someone‘); } var Employee=function(name,title){ Peron.call(this); //use constructor pattern to get the same property this.name=name; this.title=title; } Employee.prototype=Object.create(Person.protoype); Employee.prototype.constructor=Employee; //change the constructor name just habit //Same result /*var tmp=new Function(); tmp.prototype=Person.prototype Employee.prototype=new tmp(); //grail pattern to avoid the direct connection between Person and Employee Employee.prototype.constructor=Employee;*/
标签:
原文地址:http://www.cnblogs.com/thatgeek/p/4864800.html