jjzjj

prototype-chain

全部标签

javascript - 在 JavaScript 中向构造函数的原型(prototype)隐式添加方法

以下是Crockford的JavaScript:好的部分中的代码片段:Function.prototype.method=function(name,func){this.prototype[name]=func;returnthis;};Crockford继续解释"ByaugmentingFunction.prototypewithamethodmethod,wenolongerhavetotypethenameoftheprototypeproperty.Thatbitofuglinesscannowbehidden."对于这一点,我基本上是一头雾水。哪些是我们以前必须做但现在不再

javascript - 在原型(prototype)继承中实现实例方法/变量

在阅读http://javascript.crockford.com/prototypal.html之后,我一直在研究原型(prototype)继承。并且在理解如何以使用经典继承的方式使用它时遇到了一些问题。也就是说,原型(prototype)继承的所有函数和变量本质上都变成静态的,除非它们被子对象覆盖。考虑这个片段:varDepot={stockpile:[],loadAmmo:function(ammoType){this.stockpile.push(ammoType);}};varMissileDepot=Object.create(Depot);varGunDepot=Obj

javascript - Javascript 中的原型(prototype)

尽管我已经阅读了很多相关内容,但我还是无法理解原型(prototype)概念。为什么会有String和String.prototype?如果我有"cat":是String还是Object?它是否继承了String或String.prototype的所有属性/方法?为什么会有String和String.prototype?我应该将String称为String对象并将String.prototype称为String原型(prototype)吗?请澄清这一点。 最佳答案 我回答这个问题是因为这个主题中有很多错误信息:IsthataStri

javascript - 原型(prototype)复制 vs Object.create() vs new

我在使用继承时注意到可以通过三种方式获得相同的结果。有什么区别?functionAnimal(){}Animal.prototype.doThat=function(){document.write("Doingthat");}functionBird(){}//ThismakesdoThat()visibleBird.prototype=Object.create(Animal.prototype);//Solution1//Youcanalsodo://Bird.prototype=newAnimal();//Solution2//Or://Bird.prototype=Anima

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 - Cloud Code Parse Limit 1000 通过 Chaining 克服了吗?

我在下面使用以下函数来确定用户在记分牌中的排名。Parse.Cloud.define("getUserGlobalRank",function(request,response){varusernameString=request.params.username;varscoreAmount=request.params.score;varglobalRankQuery=newParse.Query("scoreDB");globalRankQuery.greaterThanOrEqualTo("score",scoreAmount);globalRankQuery.descendin

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;