草庐IT

Prototype

全部标签

javascript - jquery "this"事件处理程序的绑定(bind)问题(相当于原型(prototype)中的 bindAsEventListener)

在jquery中,事件hadler的绑定(bind)是事件生成DOM元素(this指向dom元素)。在原型(prototype)中更改事件处理程序的绑定(bind)可以使用bindAsEventListener功能;如何从事件处理程序访问实例和DOM元素?类似于HowcanIbindaneventhandlertoaninstanceinJQuery?functionCar(){this.km=0;$("#sprint").click(this.drive);//setupeventhandler}//eventhandler//initIneedtoaccessboththeclic

javascript - 使用原型(prototype)继承的javascript代码中的对象生命周期是什么?

我目前正在阅读“JavascriptGoodParts”,我遇到了以下段落Ifwetrytoretrieveapropertyvaluefromanobject,andiftheobjectlacksthepropertyname,thenJavaScriptattemptstoretrievethepropertyvaluefromtheprototypeobject.Andifthatobjectislackingtheproperty,thenitgoestoitsprototype,andsoonuntiltheprocessfinallybottomsoutwithObjec

javascript - Backbone Collection 和 Marionette CompositeView 中未定义的模型原型(prototype)

尝试从值列表填充集合时,我收到有关集合的model的prototype未定义的错误。看着thisquestionaboutasimilarproblem,我已经尽我最大的能力检查了模型是否在实例化集合之前实际创建。在从服务器获取数据并尝试使用来自应该填充到其中的数据。注意:使用Backbone0.9.10模型MyItemModel=Backbone.Model.extend({});收藏MyCollection=Backbone.Collection.extend({model:MyItemModel});CompositeView的相关代码MyCompositeView=Backbo

javascript - 用 sinon stub 一个原型(prototype)方法

假设我有以下方法:Controller.prototype.refresh=function(){console.log('refreshing');}Controller.prototype.delete=function(object){varself=this;object.delete({id:object.id},function(){self.refresh();});}现在在我的(mocha)测试中:beforeEach(function(){varcontroller=newController();varproto=controller.__proto__;varob

javascript - 可以从函数原型(prototype)访问私有(private)构造函数范围的变量吗?

根据我对javascript的理解,原型(prototype)方法不能访问构造函数范围内私有(private)的变量,varFoo=function(){varmyprivate='Iamprivate';this.mypublic='Iampublic';}Foo.prototype={alertPublic:function(){alert(this.mypublic);}//willworkalertPrivate:function(){alert(myprivate);}//won'twork}这很有道理,但有没有什么安全且好的方法可以解决这个问题?由于使用原型(prototy

javascript - 学习.prototype

编辑:对于那些将来看到这篇文章的人,thissite毫无疑问,这对我消化Javascript至关重要。如果您来自传统的OOP背景,我强烈推荐它。UML-esq图非常棒。我仍然无法理解Javascript中的.prototype属性是什么。它只是对另一个对象的引用吗?或者它是指向另一个对象的指针的引用?我来自C/C++/x86,只是看不到它是如何工作的。让我们看一些我目前如何看待事物的例子;它有助于指出我的错误,看看事情是如何工作的。我什至不知道其中一些是否是有效的语法。Object和Function分别是全局对象/函数对象。1//Global.prototype=??2//Functi

javascript - new Array() 与 Object.create(Array.prototype)

天真的困惑:vararr1=newArray();vararr2=Object.create(Array.prototype);//Insertingelementsin"botharrays"arr1[0]=0;arr1[9]=9;arr2[0]=0;arr2[9]=9;arr1.push(10);arr2.push(10);console.log(arr1.length);//prints11console.log(arr2.length);//prints1这两个对象都继承了Array.prototype,但它们使用[]运算符的行为不同。为什么? 最佳

javascript - 自定义错误和 bluebird 的 catch with ErrorClass 导致无意行为

我正在尝试为自定义错误实现一个模块。应该可以使用此模块在应用程序的要求声明中实例化单个错误:varMyCustomError=require('custom-error')('MyCustomError');这是模块:'usestrict';var_CACHE={};functioninitError(name){functionCustomError(message){this.name=name;this.message=message;}CustomError.prototype=Object.create(Error.prototype);CustomError.prototy

javascript - 从构造函数中将方法附加到原型(prototype)

这是描述JavaScript中“类”或构造函数的教科书标准方法,直接来自JavaScript权威指南:functionRectangle(w,h){this.width=w;this.height=h;}Rectangle.prototype.area=function(){returnthis.width*this.height;};我不喜欢这里的悬空原型(prototype)操作,所以我试图想办法将area的函数定义封装在构造函数中。我想到了这个,但我不期望它能工作:functionRectangle(w,h){this.width=w;this.height=h;this.con

javascript - JavaScript 的分层状态机

我对分层状态机非常感兴趣,尤其是在JavaScript中,我发现了this框架,喜欢它的外观。但是我不确定它是否可以执行分层操作。有人知道层次结构状态机的解决方案吗? 最佳答案 如果您想要类似thisarticle中描述的模式,看起来您链接的框架可以做到这一点:StateMachineClassesYoucanalsoturnallinstancesofaclassintoanFSMbyapplyingthestatemachinefunctionalitytotheprototype,includingyourcallbacksi