JavaScript里没有继承关键字,想要继承一个类需要用到“对象冒充”。5 O8 ^6 N9 g7 v" I9 Q
- <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>
1 N+ h' k9 N5 Q Z+ m4 a( J$ ?
1 S' Y: s3 A2 H5 ^8 Q( [ |