zhuzhijia1985 2010-05-20 09:47
浏览 204
已采纳

麻烦各位大虾,帮我看看这个代码,为什么红色部分不能调用?

// 获取一个类typeof的方法 function getType(x) { if(x == null) return "null"; var t = typeof x; if (t != "object") {return t;} var c = Object.prototype.toString.apply(x); //[object Class] c = c.substring(8, c.length-1); // [Class] if (c != "Object") return c; if(x.constructor == Object) return c; if ("classname" in x.constructor.prototype && typeof x.constructor.prototype.classname == "string" ) return x.constructor.prototype.classname; } //从一个类中复制方法到另一个类中 function borrowMethods(borrowForm,addTo) { var from = borrowForm.prototype; var to = addTo.prototype; for (m in from) { if(typeof from[m] != "function") continue; to[m] = from[m] } } //test的构造函数 function test(name) { this.name = name; } //为test原型添加方法,一种是直接调用getType()方法,一种是把getType()方法直接复制到test中 test.prototype.gettype= function() { [color=red]//getType.call(this,(new test())); getType.call((new test()),(new test())); borrowMethods(getType,test);[/color] } alert((new test()).gettype); //去掉"()"这种写法只是表示取对象的值,而不是调用方法,因为方法本身也就是一种属性。
  • 写回答

2条回答 默认 最新

  • myali88 2010-05-20 10:01
    关注

    [code="javascript"]
    alert((new test()).gettype);
    [/code]
    这个只是对该方法的一个引用,自然打印出来的就是gettype函数本身的内容了。如果改成[code="java"script]alert((new test()).gettype());[/code]这样才会发起函数调用,自然才能知道红色部分是否执行了,返回值是“undefined”是正确的,因为函数没有明确的返回值。
    如果你要知道红色部分是否调用了,在getType里面设置断点就可以看到,或者里面加个什么alert之类的语句。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?