草庐IT

this_is_not_a_task

全部标签

javascript - 调用 Function.prototype.method() 时 TypeError : this. prototype is undefined

我正在读《Javascript:好的部分》这本书。现在我正在阅读有关增强类型的章节:Function.prototype.method=function(name,func){this.prototype[name]=func;returnthis;};更新:为什么下面的代码不起作用?js>Function.prototype.method("test",function(){print("TEST")});typein:2:TypeError:this.prototypeisundefined但是下面的代码没有问题:js>Function.method("test",function

javascript - jquery.each 函数是否有可能不破坏 'this' 变量?

所以如果变量“this”当前被设置为一个对象,{name:"Theoldthis"}下面的代码会在循环中改变它vararray=[1,2,3];$.each(array,function(i,e){alert(this.name);});不会找到this.name,而是在循环执行期间将变量“this”设置为与“e”相同是否可以让jquery不破坏$.each循环中的this变量? 最佳答案 如果您使用native.forEach而不是$.each,您可以通过发送第二个回调来设置回调的this值争论...array.forEach(f

javascript - Uncaught ReferenceError : Class is not defined?

我很想知道为什么会出现此错误?我已经从网站beezid.com(carousel.js)文件中复制了它,并且我正在尝试将与他们相同的幻灯片放到我的网站上以更新它。如您所见我对此有疑问??他们的网站没有出现这个错误?carousel.js:26UncaughtReferenceError:ClassisnotdefinedCarousel=Class.create(Abstract,{initialize:function(scroller,slides,controls,options){this.scrolling=false;this.scroller=$(scroller);th

javascript - 是什么导致某些页面上的 jQuery "$(...) is null"?

我有一些控制隐藏div的javascript。现在它在大多数页面上都有效,但是在其他带有其他javascript的页面上它不起作用...我的js写得不好吗?$(document).ready(function(){$("#user-dropdown-toggle").live('click',function(){$("#left-user-bar").addClass("open");$("#user-dropdown-toggle").addClass("league-judgement");$("body").addClass("league-judgement");});$("

十分钟 Javascript : What is going on in this example code illustrating lazy scoping?

我一直在重读SpencerTipping的优秀作品JavascriptinTenMinutes在这个使用惰性作用域创建语法宏的示例中,我终究无法弄清楚发生了什么:varf=function(){return$0+$1};varg=eval(f.toString().replace(/\$(\d+)/g,function(_,digits){return'arguments['+digits+']'}));g(5,6);//=>11(exceptonIE)特别是,$0和$1正在被一个函数定义取代——那个函数是如何被计算的?(大概是通过eval(),但我没有看到)。函数中单个下划线参数的用

javascript - 有条件 "not equal to a and not equal to b"等的 JS 简写吗?

我只是想知道是否有一些JS简写是这样的:if(x!=1&&x!=2)dostuff;这样的野兽存在吗?相反,我想说这样的话:if(x!=1:2)dostuff; 最佳答案 不,没有这样的速记。如果不想重复变量,可以使用开关:switch(x){case1:case2:break;default:dostuff;}另一种方法是在数组中查找值:if([1,2].indexOf(x)==-1)dostuff;但是,Array.indexOf并非在所有浏览器中都存在,因此您可能需要一个后备方案,例如您可以在MozillaArray.inde

javascript - "this"的范围

我有一个简单的对象,我不明白this的概念(作用域)是通过调用这个对象的函数来实现的。为什么在最后一个变体(3)中调用show()(使用函数show()insideobjectwithoutparent)结果是“Thisisglobal”并且不是内部变量title("ColorPicker")?我有一个模糊的想法,即在定义全局变量show之后调用函数popup.show(),this指的是全局对象。这是逻辑解释吗?代码:http://jsbin.com/otuzac/1/edit.vartitle="'This'isglobal";varpopup={dom_element:("#po

javascript - 为什么使用 .call(this) 而不是括号

这个问题在这里已经有了答案:Reasonbehindthisselfinvokinganonymousfunctionvariant(5个答案)关闭8年前。有没有什么特别的原因让我经常遇到:(function(){console.log("Hello");}).call(this);代替:(function(){console.log("Hello");})();传不传this调用应该是一样的效果吧?似乎有一些性能差异:http://jsperf.com/call-vs-parenthesis.

javascript - TypeError : $(. ..).style is undefined - 错误还是我的错?

我一开始只想改变一个元素的背景,但后来遇到了这个:TypeError:$(...).styleisundefined(在Firefox控制台中)HTML:LauraSack-OffizielleWebseiteJavascript:$(document).ready(function(){$("#gallery-container").style.background="black";}); 最佳答案 您正在将javascript与jquery混合使用。在jquery中你必须使用css()让它像这样工作:$("#gallery-co

javascript - “等于”未定义 : Ember-qunit does not seem to be importing

Qunit测试方法似乎不可用,尽管我很确定我正在正确导入它们。我收到以下错误:unit/models/friend-test.js:line11,col3,'ok'isnotdefined.unit/models/friend-test.js:line17,col3,'equal'isnotdefined.unit/models/friend-test.js:line23,col3,'equal'isnotdefined.unit/models/friend-test.js:line31,col3,'equal'isnotdefined.unit/models/friend-test.