js设计模式6 prototype

var Car = {
  		"drive":function(){
  			alert("im driving!");
  		},
  		"stop":function(){
  			alert("stopped");
  			},
  		"showName":function(){
  			alert("my name is"+this.name);
  			}
  		}
  	var bmw = Object.create(Car,
  		{"name":{"value":"baoma","enumerable":true,"writable":false,"configurable":false},
  			"stop":{"value":function(){alert("fuckoff");}}
  			});
        //writable 和configurable是默认值
        bmw.drive();
  	bmw.showName();
  	bmw.stop();
var vehicle = {
  		"init":function(str){
  			this.name = str
  			},
  		"getName":function(){
  			alert(this.name);
  			}
  		}
  	function getPrototype(str){
  		function F(){};
  		F.prototype = vehicle;
  		var f = new F();
  		f.init(str);
  		return f;
  		}
  	var myCar = getPrototype("hehe");
  	myCar.getName();


你可能感兴趣的:(js设计模式6 prototype)