草庐IT

enable_shared_from_this

全部标签

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(正则表达式): excluding matches from output

我有这个文本:nisinontext600elit其中600是动态添加的。我怎样才能得到它?varstr=('nisinontext600elit').match(/text\d+/);alert(str);这会提醒text600,我如何才能只提醒600而无需额外替换单词text(如果可能的话)?感谢任何帮助,谢谢! 最佳答案 varstr=('nisinontext600elit').match(/text(\d+)/);alert(str[1]); 关于JavaScript(正则表达

十分钟 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 - SlickGrid 列选择器 : Setting the default columns to display from a larger list

我目前正在使用SlickGrid并允许用户使用ColumnPicker选择要显示的列。按照http://mleibman.github.com/SlickGrid/examples/example4-model.html的例子我已经能够让它很好地工作。我不确定的下一步是是否可以选择默认的列列表以在首次渲染时显示。例如,假设我有一个包含5列的数组,声明如下:{name:"Name"field:"Name"id:"Name"sortable:trueminWidth:120editor:Slick.Editors.Text},{name:"Address"field:"Address"id

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 - CKEditor 4 : How to add CSS stylesheet from plugin?

暂时试过了,但是没有成功editor.addCss(this.path+'tabber.css');editor.document.appendStyleSheet(this.path+'tabber.css');完整代码(function(){CKEDITOR.plugins.add('tabber',{init:function(editor){editor.ui.addButton('addTab',{command:'addTab',icon:this.path+'icons/tabber.png',label:Drupal.t('Inserttabs')});editor.a

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

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

javascript - 如何从 promise 方法内部访问 Angular js Controller 的 "this"?

我有一个简单的AngularjsController,如下所示发出XHR请求app.controller('MainController',['$http',function($http){this.php_response={};varpromise=$http.get('process.php');promise.then(function(success_data){//Idontthink"this"istalkingtothecontrollerthisanymore?this.php_response=success_data;},function(error){conso

javascript - Webpack 未捕获引用错误 : require is not defined after removing node_modules from bundle. js

bundle.js2.83kB0[emitted]mainbundle.js.map3.36kB0[emitted]main当我将下面的代码添加到自定义外部时,我可以删除node_modules,使其不直接包含在bundle.js输出中。bundle.js743kB0[emitted]mainbundle.js.map864kB0[emitted]main这显着减小了包的大小。但我在浏览器中收到一条错误消息:UncaughtReferenceError:requireisnotdefined在浏览器中。有谁知道如何解决这个问题?varpath=require("path"),fs=re

javascript - 在 IIFE 上使用 .call(this)

我见过用.call(this)包裹的IIFE,而不仅仅是()。为什么要这样做?上下文:https://github.com/dmauro/Keypress/blob/development/keypress.js(据我了解,this将引用window对象-无论如何都会调用IIFE。这似乎是多余的。) 最佳答案 如果不使用call调用IIFE,它里面的this会引用全局对象window(或者undefined在严格模式下)。使用.call(this)将传递给它任何this在调用时引用的内容(无论当前上下文是什么)。例如,您想使用.ca