Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Last active August 29, 2015 14:02
Show Gist options
  • Save jikeytang/441222c0a9747b8ff015 to your computer and use it in GitHub Desktop.
Save jikeytang/441222c0a9747b8ff015 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140620-题目1
用代码实现函数B继承函数A里边的全部属性与方法,且覆盖函数A其中的sayName方法。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@hjzheng
Copy link

hjzheng commented Jun 23, 2014

function A(name){
    this.name = name;
}

A.prototype.sayName = function(){
    console.log("this is A sayName method");
    console.log(this.name);
}

function B(name){
    A.call(this, name);
}


B.prototype = new A();

B.prototype.sayName = function(){
    console.log("this is B sayName method");
    console.log(this.name);
}

var b = new B("b");
b.sayName();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment