JavaScript里没有继承关键字,想要继承一个类需要用到“对象冒充”。 p' R& Y8 Q: ^9 r& {. \
- <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>
v# t! R% L z5 l) ^
) [2 l4 q4 S6 s6 s: Z: t |