JavaScript里没有继承关键字,想要继承一个类需要用到“对象冒充”。
, w h: Z7 i) R3 c9 M<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>
9 a8 V6 B7 z1 o& E2 y& a7 p
: A- N$ e9 m8 x* ^' I7 _ |