职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1388|回复: 4

诸多问题的正方形继承矩形

[复制链接]
木已 发表于 2011-9-3 10:51 | 显示全部楼层 |阅读模式
Javascript代码  
Object.prototype.extend = function (Parent) {   
    this.prototype = new Parent();   
}   
Rectangle = function (width, height) {   
    this.width = width;   
    this.height = height;   
}   
Rectangle.prototype.area = function () {   
    return this.width * this.height;   
}   
Square = function (width) {   
    this.__proto__.width = width;   
    this.__proto__.height = width;   
    this.whoami = function () {   
        return "I am a Square.";   
    }   
}   
Square.extend(Rectangle);   
  
var s = new Square(5);   
document.writeln(s.area());  // The output is 25.   
document.writeln(s.whoami()); // The output is "I am a Square."  

Object.prototype.extend = function (Parent) {
    this.prototype = new Parent();
}
Rectangle = function (width, height) {
    this.width = width;
    this.height = height;
}
Rectangle.prototype.area = function () {
    return this.width * this.height;
}
Square = function (width) {
    this.__proto__.width = width;
    this.__proto__.height = width;
    this.whoami = function () {
        return "I am a Square.";
    }
}
Square.extend(Rectangle);

var s = new Square(5);
document.writeln(s.area());  // The output is 25.
document.writeln(s.whoami()); // The output is "I am a Square."


以上代码存在很多问题,不知你能说出几个?

醉倚西风 发表于 2011-9-3 10:51 | 显示全部楼层
this.__proto__啊……还真有人会在编程过程中用到这东西……

能文能武 发表于 2011-9-3 10:51 | 显示全部楼层
在prototype.method用this,还有this.__proto__。。。
dfbcfgbfg 发表于 2011-12-3 22:57 | 显示全部楼层
不错..........
dfbcfgbfg 发表于 2011-12-3 22:57 | 显示全部楼层
不错..........
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-26 06:24 , Processed in 0.126287 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表