草庐IT

strict-prototypes

全部标签

javascript - 为什么不应该向 JavaScript 构造函数添加功能,而是通过原型(prototype)添加功能?

我在看AddyOsmani关于构造函数模式的章节:http://addyosmani.com/resources/essentialjsdesignpatterns/book/#constructorpatternjavascript我遇到了以下情况:functionCar(model,year,miles){this.model=model;this.year=year;this.miles=miles;this.toString=function(){returnthis.model+"hasdone"+this.miles+"miles";};}//Usage://Wecancr

Javascript:函数.原型(prototype).方法

我想你们中的大多数人都看过以下代码片段:Function.prototype.method=function(name,func){this.prototype[name]=func;returnthis;};我也知道它会影响所有函数,因为它们都是由Function创建的对象,因此它们可以访问名为“method”的方法,但是我很困惑为什么Function本身也可以像下面这样访问“method”:Function.method('test',function(){return1;}); 最佳答案 Edorka的回答是正确的:函数是它自

javascript - 如何在 JavaScript 中为 Number.toFixed 编写原型(prototype)?

我需要使用JavaScript将小数四舍五入到六位,但我需要考虑旧版浏览器,所以我can'trelyonNumber.toFixedThebigcatchwithtoExponential,toFixed,andtoPrecisionisthattheyarefairlymodernconstructsnotsupportedinMozillauntilFirefoxversion1.5(althoughIEsupportedthemethodssinceversion5.5).Whileit'smostlysafetousethesemethods,olderbrowsersWILL

javascript - 为什么instanceof在原型(prototype)改变后一直说真?

instanceof运算符应该看看原型(prototype),不是吗?为什么在更改对象的原型(prototype)后它不更改答案?示例如下://The.prototypeofobjectscreatedwith'newMyKlass'//isMyKlass.prototypevarMyKlass=function(name,age){this.name=name;this.age=age;}varxx=newMyKlass('xx',20);console.log(xxinstanceofMyKlass);//true,OKxx.prototype=newString('s');con

javascript - 使用原型(prototype)/"new"的继承

这个问题在这里已经有了答案:Whywouldn'tIuseChild.prototype=Parent.PrototyperatherthanChild.prototype=newParent();forJavascriptinheritance?(3个答案)关闭7年前。大家好,我是JavascriptOO的新手,想了解更多关于继承的知识。希望大家多多指教!我看到这篇很棒的帖子:Howto"properly"createacustomobjectinJavaScript?它讨论了我在其他网站上看到的类是如何继承的,例如:functionman(x){this.x=x;this.y=2;

javascript hasOwnProperty 和原型(prototype)

functionAnimal(name,numLegs){this.name=name;this.numLegs=numLegs}Animal.prototype.sayName=function(){console.log("Himynameis"+this.name);}varpenguin=newAnimal("CaptainCook",2);penguin.sayName();for(varpropinpenguin){console.log(prop);}penguin.hasOwnProperty('sayName')结果:namenumLegssayName=>false

javascript - 将原型(prototype)附加到 JavaScript 对象

假设我有以下带有人员对象的JSON。[{"name":"Alice","age":28,},{"name":"Bob","age":33}]如果我解析它,我将得到一个包含两个JavaScript对象的数组。比方说,我想为这两个对象配备一个名为introduce的方法,它会做类似这样的事情functionintroduce(){return"Mynameis"+this.name+"andIam"+this.age+"yearsold.";}现在,我可以遍历我的两个人并调用person.constructor.prototype.introduce=introduce但是,这将在所有Ja

javascript - Rebol 真的有 javascript 原型(prototype)属性的等价物吗?

Gregogy在这里发表了一篇关于rebol和javascript的文章http://blog.revolucent.net/2009/05/javascript-rebol.html但是随着我深入比较javascript和rebol,我看不出什么是javascript原型(prototype)的rebol等价物。因为在rebol中使用make从另一个对象实例扩展对象实例并不完全像javascript原型(prototype)属性,因为js原型(prototype)允许一次扩展所有实例。我是不是弄错了,或者是否有与下面的rebol代码等效的代码:functionPerson(first

javascript - 删除事件监听器作为 Class.prototype 函数

我试图在我的项目中使用基于Class.prototype的类,但我没有内联函数。考虑到这个例子,不可能删除我在类里面的myVideo视频对象上的eventListener。这是一个理论示例,不是我拥有的实际生产代码。varmyClass=function(){this.initialize();}MyClass.prototype.myVideo=null;MyClass.prototype.initialize=function(){this.myVideo=document.getElementById("myVideo");this.myVideo.addEventListene

javascript - 在构造函数*内部*分配原型(prototype)方法——为什么不呢?

在风格上,我更喜欢这种结构:varFilter=function(category,value){this.category=category;this.value=value;//productisaJSONobjectFilter.prototype.checkProduct=function(product){//runsomechecksreturnis_match;}};对于这个结构:varFilter=function(category,value){this.category=category;this.value=value;};//varFilter=function