原型继承总结
2017-04-12 14:39
363 查看
<!DOCTYPE html> <!--1.原型链--> <!--2. call apply-->
<!--3. object.create()--><!--4.只继承原型-->
<!--5.浅拷贝深拷贝--> object.assign() 实现浅拷贝
<!--本总结暂时只讨论1-2-3-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
//-----------------------one part----------------------- function Person(first,last) { this.first = first; this.last = last; this.fullNameinstance = function () { return this.first+' instance '+this.last; } } Person.prototype = { fullName:function () { return this.first+' proto '+this.last; }, fullNameReversed:function () { return this.last+','+this.first; } }; var dw = new Person("din","wan"); alert(dw.fullNameinstance()); alert(dw.fullName()); function Goodperson(first,last) { this.first = first; this.last = last; this.goodfullnameinstance = function () { return this.first + 'good instance ' + this.last; } }; Goodperson.prototype = new Person(); Goodperson.prototype.constructor = Goodperson; Goodperson.prototype.goodfullname = function () { //这个一定要写到前面这行代码的后面 否则会出错 return this.first + ' good pro ' + this.last; }; // Goodperson.prototype = { 这里是错误的 这是全覆盖 如果这样写后面的 good.fullname()就不能访问 // goodfullname:function () { // return this.first + 'good' + this.last; // } // }; var good = new Goodperson("dada","wang"); alert(good.fullNameinstance()); alert(good.fullName()); alert(good.goodfullnameinstance()); alert(good.goodfullname()); //-----------------------two part----------------------- function Person(first,last) { this.first = first; this.last = last; this.fullNameinstance = function () { return this.first+' instance '+this.last; } }; Person.prototype = { fullName:function () { return this.last+'proto'+this.first; }, fullNameReversed:function () { return this.last+','+this.first; } }; var dw = new Person("din","wan"); alert(dw.fullNameinstance()); //--din instance wan alert(dw.fullName()); //--din proto wan function Student(first,last,id) { Person.call(this,first,last); //call是function才有的 所以Person.prototype.call(this,first,last); 是不行的 也就是说call不能指向Person 的 prototype this.id = id; this.studentnameinstance = function () { return this.first + 'student instance ' + this.last; } }; Student.prototype = new Person(); // Student.prototype = { // getid:function () { // return this.id; // } // }; //如果这样写就会有问题 Student.prototype.getid = function () { return this.id; }; Student.prototype.studnetnamePro = function () { return this.first + ' student pro ' + this.last; }; var dinwan = new Student("din","wan","21"); alert(dinwan.fullNameinstance()); //--din instance wan alert(dinwan.fullName()); //--din proto wan alert(dinwan.studentnameinstance()); //--din student instance wan alert(dinwan.studnetnamePro()); //--din student pro wan alert(dinwan.getid()); //--21
//----------------------------three part-------------------------------
function Shape() {
this.x = 0;
this.y = 0;
}
Shape.prototype.move = function(x,y) {
this.x += x;
this.y += y;
console.info("Shape moved.")
} ;
function Reatangle() {
Shape.call(this); //call super constructor.
}
Rectangle.prototype = Object.create(Shape.prototype);
var rect = new Rectangle();
rect instanceof Rectangle //true.
rect instanceof Shape //true.
rect.move(1,1); //Outputs,"Shape moved"</script>
</body>
</html>
相关文章推荐
- JavaScript原型链与继承操作实例总结
- 栋栋晓13:Javascript学习总结:原型、原型链和继承。
- javascript原型和继承总结
- 原型继承基础知识总结
- 面向对象继承总结:拷贝继承、类式继承和原型继承
- JavaScript原型、闭包、继承和原型链等等总结
- js原型、constructor、继承总结
- JS 面向对象编程、原型链、原型继承(个人学习总结)
- JavaScript原型链与继承内容总结
- JavaScript 原型总结六 继承
- javascript基础学习三:原型继承
- 关于C++中的虚拟继承的一些总结
- JS中实现继承的几种方式总结
- JavaScript OOP:继承与原型链
- 动态原型继承问题
- javascript中的原型继承
- SSH——Hibernate继承关系映射详解(配置+实例+总结)
- C++继承部分总结
- 继承与原型链
- Java继承的总结