Inheritance(Chapter 5 of JavaScript: The Good Parts)
2010-07-10 11:46
501 查看
code 1 var Mammal = function (name) {
2 this.name = name;
3 };
4
5 Mammal.prototype.get_name = function () {
6 return this.name;
7 }
8
9 Mammal.prototype.says = function () {
return this.saying || '';
};
var myMammal = new Mammal('Herb the Mammal');
var name = myMammal.get_name();
alert(name);
var Cat = function (name) {
this.name = name;
this.saying = 'meow';
};
Cat.prototype = new Mammal();
Cat.prototype.purr = function (n) {
var i, s = '';
for (i = 0; i < n; i += 1) {
if (s) {
s += '-';
}
s += 'r';
}
return s;
};
Cat.prototype.get_name = function () {
return this.says() + ' ' + this.name + ' ' + this.says();
};
var myCat = new Cat('Henrietta');
alert(myCat.says());
alert(myCat.purr(5));
alert(myCat.get_name());
2 this.name = name;
3 };
4
5 Mammal.prototype.get_name = function () {
6 return this.name;
7 }
8
9 Mammal.prototype.says = function () {
return this.saying || '';
};
var myMammal = new Mammal('Herb the Mammal');
var name = myMammal.get_name();
alert(name);
var Cat = function (name) {
this.name = name;
this.saying = 'meow';
};
Cat.prototype = new Mammal();
Cat.prototype.purr = function (n) {
var i, s = '';
for (i = 0; i < n; i += 1) {
if (s) {
s += '-';
}
s += 'r';
}
return s;
};
Cat.prototype.get_name = function () {
return this.says() + ' ' + this.name + ' ' + this.says();
};
var myCat = new Cat('Henrietta');
alert(myCat.says());
alert(myCat.purr(5));
alert(myCat.get_name());
相关文章推荐
- Methods(Chapter 8 of JavaScript: The Good Parts)
- Functions part1(Chapter 4 of JavaScript: The Good Parts)
- Arrays(Chapter 6 of JavaScript: The Good Parts)
- Objects(Chapter 3 of JavaScript: The Good Parts)
- Functions part2(Chapter 4 of JavaScript: The Good Parts)
- Regular Expressions(Chapter 7 of JavaScript: The Good Parts)
- Grammar(Chapter 2 of JavaScript: The Good Parts)
- Style(Chapter 9 of JavaScript: The Good Parts)
- JavaScript- The Good Parts Chapter 3 Objects
- JavaScript- The Good Parts CHAPTER 2
- Bad Parts(Appendix B. of JavaScript: The Good Parts)
- javascript the good parts Chapter 4 Functions
- JavaScript- The Good Parts Chapter 4
- Awful Parts(Appendix A. of JavaScript: The Good Parts)
- JavaScript- The Good Parts Chapter 6
- OReilly JavaScript The Good Parts 关于Object.prototype和delete
- Learning Notes - Understanding the Weird Parts of JavaScript
- JavaScript:The Good Parts学习笔记1
- 读 《JavaScript: The Good Parts》 有感
- JavaScript.The.Good.Parts阅读笔记(一)假值与===运算符