jjzjj

prototype

全部标签

javascript - 分配给 __proto__ 属性的正确方法

我有一些从JSON反序列化的对象,我想为其分配一个新原型(prototype)以提供各种getter和setter函数。执行此操作的明显方法(如thisquestion中所述)是设置myJsonObj.__proto__={function1:/*...*/,function2:/*...*/};但是,作为MDChelpfullypointsout,__proto__属性是非标准的并且已弃用。是否有任何符合标准的方式(对于“标准”的某些定义)来实现相同的效果,而无需创建大量新的包装器对象? 最佳答案 没有符合标准的方法可以在对象创建

JavaScript 继承 : When where's my derived members?

看看下面的代码:functionPrimate(){this.prototype=Object;this.prototype.hairy=true;}functionHuman(){this.prototype=Primate;}newHuman();当您检查newHuman()时,没有hairy成员。我希望会有一个。有没有其他方法可以让我从Primate继承?涉及Object.create()的内容(ECMAScript5适合在我的场景中使用)? 最佳答案 在编写代码时,使用newHuman()创建的对象将具有一个名为protot

javascript - 原型(prototype)继承的差异,Firefox 与 Chrome

对于下面的代码:functionMammal(){this.hair=true;this.backbone=true;returnthis;}functionCanine(){this.sound='woof';returnthis;}Canine.prototype=newMammal();functionDog(name){this.tail=true;this.name=name;returnthis;}Dog.prototype=newCanine();varaspen=newDog('Aspen');varaspenProto=aspen.__proto__Firebug(F

javascript - 在 JavaScript 事件中替换/覆盖/覆盖 e.target

有一个JSFiddlehere,你能在不克隆到新对象的情况下替换e.target吗?下面重复了那个fiddle的听众;one.addEventListener('click',function(e){//defaultbehaviour,don'tmodifytheeventatalllogTarget(e);});two.addEventListener('click',function(e){//replacethevalueonthesameobject,whichseemstoberead-onlye.target=document.createElement('p');log

javascript - 为什么 native 数据类型属性未显示在其相应的原型(prototype)上?

例如,Array数据类型有一个名为pop()的函数,我想它是使用以下方法添加的:Array.prototype.pop=function(){/*...*/};但据我所知,使它不可枚举的唯一方法是做这样的事情:Object.defineProperty(Array.prototype,"pop",{enumerable:false});并非所有浏览器都支持。Array.prototype.doSomething=function(){};vararr=[];console.log(arr);//[doSomething:function]那么为什么doSomething出现在这里,而p

javascript - 如何重新实现 'var that = this' 以使用 Object.prototype.bind() 保存范围引用?

在SecretsofJavascriptClosures,StuartLangridge提供了一段代码来演示闭包在.onclick回调中的常见用法,并解释如下:link.onclick=function(e){varnewa=document.createElement("a");varthat=this;document.body.appendChild(newa);newa.onclick=function(e){that.firstChild.nodeValue="reset";this.parentNode.removeChild(this);}}我最近偶然发现了KyleSim

javascript - 在闭包内声明的类与没有闭包的标准类

通常我使用基于原型(prototype)的标准OOP方法,我的类看起来像这样varstd=function(){this.log=function(msg){console.log("wanttobeprivate."+msg)};};std.prototype={logInfo:function(msg){this.log(msg);}};但在那种情况下,log是公共(public)方法,任何人都可以使用它。但我想将其设为私有(private),但在原型(prototype)中声明的方法中仍然可用。为此,我们需要闭包。代码会改成这样varclosureStd=(function(){

console.log 中的 JavaScript 对象输出

我想知道打印对象时console.log从哪里得到构造函数的名字。另外,这实际上对代码有什么影响吗?functionF(){this.test='ok';}varf=newF();console.log(f);console.log(在Chrome中)的输出是:F{测试:“确定”}console.log从哪里得到F{test...中的F?如果我将F.constructor、F.prototype和f.constructor更改为随机值,它仍会打印原始的F:functionG(){this.fail='bad';}functionF(){this.test='ok';}F.prototy

javascript - 变量隐式声明和原型(prototype)

尽量保持简短。使用phpstorm查看我的代码并发现了一些错误。它说我的函数命名位置有一个“隐式声明的变量”functiontowngate10(){updateDisplay(locations[10].description);if(!gate10){score=score+5;gate10=true;}playerLocation=10;displayScore();document.getElementById("goNorth").disabled=true;document.getElementById("goSouth").disabled=false;document.

javascript - 难以手动走原型(prototype)链

我想尝试手动遍历几个对象的原型(prototype)链,看看我在这个过程中找到了什么。但是,我卡在了我尝试的第一个上。这是代码:functionMyObject(){}varx=newMyObject();console.log('--------------------------------------------');console.log('x.constructor.name:'+x.constructor.name);console.log('x.constructor.prototype.constructor.name:'+x.constructor.prototype