JavaScript里没有继承关键字,想要继承一个类需要用到“对象冒充”。
) o$ w' g1 i( [( x* L9 j<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>
% M7 A3 }( ~0 @5 i0 S8 I5 l# k6 J: m! M- U
|