JavaScript里没有继承关键字,想要继承一个类需要用到“对象冒充”。
9 C; s( T7 C* F8 Y2 j& Z2 o<script type = 'text/javascript'>
function Person(name , age){
this.name = name ;
this.age = age ;
this.say = function (){
return "name : "+ this.name + " age: "+this.age ;
} ;
}
var o = new Object() ;//可以简化为Object()
Person.call(o , "zhangsan" , 20) ;
console.log(o.say() );//name : zhangsan age: 20
</script> % A( t& t5 g* [7 {9 ^6 W t: i
2 v+ T& w- u& j) G
|