原型继承总结
来源:互联网 发布:淘宝开店在哪里申请 编辑:程序博客网 时间:2024/06/07 18:36
<!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 wanalert(dw.fullName()); //--din proto wanfunction 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 wanalert(dinwan.fullName()); //--din proto wanalert(dinwan.studentnameinstance()); //--din student instance wanalert(dinwan.studnetnamePro()); //--din student pro wanalert(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>
0 0
- 原型继承总结
- js原型、constructor、继承总结
- JavaScript原型链与继承内容总结
- JavaScript原型、闭包、继承和原型链等等总结
- 原型继承
- 原型继承
- 原型继承
- 原型继承
- 原型继承
- 原型-原型链-继承
- 面向对象继承总结:拷贝继承、类式继承和原型继承
- 8-深入面向对象+原型链+继承(总结+note)
- 栋栋晓13:Javascript学习总结:原型、原型链和继承。
- JS:原型、原型链、继承
- 原型和原型链继承
- 类继承与原型继承
- 原型继承、构造函数继承
- 原型继承和Class继承
- Java多线程学习(吐血超详细总结)
- socket 指定网口收发数据
- CloseHandle(),TerminateThread(),ExitThread()的区别
- Eclipse中背景颜色调节
- 脚本错误:对象不支持“getLabel”属性或方法
- 原型继承总结
- UVALive5964 LCM Extreme && UVA11426 GCD
- 如何删除MyEclipse通过Project Facets添加的Facet
- 欢迎使用CSDN-markdown编辑器
- Unity中的协同程序
- iOS 关于pod search 出错的问题
- MATLAB-Psychtoolbox安装记录
- MySQL 远程访问
- JSTL