首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 新手问prototype [已结贴,结贴人:2303191]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • 2303191
    • 等级:
    发表于:2008-03-23 19:51:12 楼主
    function ClassA(sColor) { 
        this.name = "dddd";
    }
    function ClassB(sColor, sName) {
        this.name = sName;
    }
    ClassB.prototype = new ClassA();
    ClassB.prototype.sayName = function () {
        alert(this.name);     
    };
    var objB = new ClassB("blue", "Nicholas");

    objB.sayName();
    delete objB.sayName;
    objB.sayName();

    问:

    ClassB.prototype.sayName 这个sayName方法是加在classb对象上了还是classa对象上了,如果加在classb上,为什么用delete删除后还能输出?如果加在classa上,为什么this指向classb的name而不是classa的name??
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-03-24 01:32:131楼 得分:10
    HTML code
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta name="generator" content="editplus" /> <meta name="author" content="Gao YiXiang" /> <meta name="email" content="yixianggao@126.com" /> <meta name="keywords" content="javascript dhtml dom" /> <meta name="description" content="I love web development." /> </head> <body> <script type="text/javascript"> <!-- function ClassA(sColor) { this.name = "dddd"; } function ClassB(sColor, sName) { this.name = sName; this.color = sColor; } ClassB.prototype = new ClassA(); ClassB.prototype.sayName = function () { alert(this.name); }; var objB = new ClassB("blue", "Nicholas"); objB.sayName(); // sayName 当然是 ClassB 上的! delete objB.sayName; // 这样仅删除了当前实例对 ClassB.prototype.sayName 的引用。 //delete ClassB.prototype.sayName; // 这样才真地删除了! objB.sayName(); // 由于原型方法仍然可用,因此调用 ClassB.prototype.sayName 成功! //--> </script> </body> </html>
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-03-24 13:01:222楼 得分:10
    HTML code
    楼上正解. 事实上这个新的方法是加到A里去了。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta name="generator" content="editplus" /> <meta name="author" content="Gao YiXiang" /> <meta name="email" content="yixianggao@126.com" /> <meta name="keywords" content="javascript dhtml dom" /> <meta name="description" content="I love web development." /> </head> <body> <script type="text/javascript"> <!-- function ClassA(sColor) { this.name = "dddd"; } function ClassB(sColor, sName) { this.name = sName; this.color = sColor; } objA = new ClassA(); ClassB.prototype = objA; ClassB.prototype.sayName = function () { alert(this.name); }; var objB = new ClassB("blue", "Nicholas"); objB.sayName(); // sayName 当然是 ClassB 上的! objA.sayName();//这里say的就是classb的了。 delete objB.sayName; // 这样仅删除了当前实例对 ClassB.prototype.sayName 的引用。 //delete ClassB.prototype.sayName; // 这样才真地删除了! //delete objA.sayName; //这样也可以删除. objB.sayName(); // 由于原型方法仍然可用,因此调用 ClassB.prototype.sayName 成功! //--> </script> </body> </html>
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-03-24 13:50:283楼 得分:0
    顶.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-03-24 15:28:004楼 得分:0
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-03-24 17:26:355楼 得分:0
    -d 用jQuery
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    世纪乐知(北京)网络技术有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2007, CSDN.NET, All Rights Reserved