草庐IT

THIS_ARCH

全部标签

javascript - 为什么将字符串作为 "this"传递会导致这种奇怪现象?

我试图理解为什么javascript会做一些(对我来说)意想不到的事情。这里有一些代码,纯粹是为了举例。换句话说,我实际上并不想扩展String(我实际上绑定(bind)到函数和东西)。所以这是没有库的纯JavaScript。vars='blah';String.prototype.foo=function(){console.log('this===s:',this===s);console.log('this==s:',this==s);console.log('typeofthis:',typeofthis);console.log('typeofs:',typeofs);con

javascript - 主干错误 : Uncaught TypeError: Object function (){ parent. apply(this, arguments); } 没有方法 'on'

知道为什么我在调用collection.fetch时会收到此错误吗?在这段代码中抛出:这是触发错误的代码:$(document).ready->SearchResult=Backbone.Model.extendSearchResults=Backbone.Collection.extendurl:"/backbone/search"model:SearchResultparse:(response)->console.logresponsenewSearchResultid:response.idtitle:response.titlesearchResults=newSearchR

javascript - 为什么此代码有效 : "(1,eval)(' this')"

为什么下一个代码是有效的Javascript代码?varglobal=(1,eval)('this');alert(global); 最佳答案 那是因为commaoperator返回它的第二个操作数(并计算两者)。您问题中的代码相当于:1;varglobal=eval('this');alert(global); 关于javascript-为什么此代码有效:"(1,eval)('this')",我们在StackOverflow上找到一个类似的问题: https

javascript - react : How to access refs in this. props.children

我想调用一个子组件的函数。是否有可能在React中从this.props.children获取引用。varComponentSection=React.createClass({componentDidMount:function(){//Howtoaccessrefsinthis.props.children?this.refs.inner.refs.specificPanel.resize();},render:function(){return({this.props.children});}});varPanel=React.createClass({resize:functi

javascript - 为什么箭头函数的 'this' 在嵌套对象字面量中没有变化?

这个问题在这里已经有了答案:MethodsinES6objects:usingarrowfunctions(6个答案)关闭6年前。我发现在嵌套对象文字中使用箭头函数时,“this”关键字似乎总是指向global。根据其他问题,以下代码片段可以解释为箭头函数的“this”是在词法上下文中定义的。varc=100;vara={c:5,fn:()=>{returnthis.c;}};console.log(a.c);//100但是,我无法理解以下代码(嵌套对象字面量):varc=100;vara={c:5,b:{c:10,fn:()=>{returnthis.c;}}}console.log

javascript - backbone.js View 继承。 `this` 父级分辨率

我有一个使用View继承的案例,我的代码基本上是这样的:parentView=Backbone.View.extend({events:{"someevent":"business"},initialize:function(){_.bindAll(this);},business:function(e){...this.someFunc&&this.someFunc();...}});childView=parentView.extend({events:{...},constructor:function(){this.events=_.extend({},parentView.p

javascript - Typescript 错误引用 _this

我尝试在TypeScript中为String.Prototype定义一些属性:Object.defineProperty(String.prototype,'test',{value:()=>{console.log("thisisatestovertext"+this);}})在javaScript原型(prototype)中,this指调用方法的对象(在本例中为字符串值)。但是文件的编译输出是:var_this=this;Object.defineProperty(String.prototype,'test',{value:function(){console.log("this

javascript - this.state 与 React 中的状态

我在一个新的代码库中工作。通常,我会在React组件中设置这样的状态:classAppextendsReact.Component{constructor(){super();this.state={foo:'bar'}}....在这个新的代码库中,我看到了很多这样的东西:classAppextendsReact.Component{state={foo:'bar'}....这样做有好处吗?他们似乎只在不需要更改状态时才这样做。我一直认为状态是React处理的东西。这是一件好事吗? 最佳答案 两种方法的最终结果是相同的。这两种方法都

javascript - this 关键字是构造函数中的 window 对象

好吧,所以我以为我理解了这一点(没有双关语的意思),但显然不是。varConstructor=function(){varinternalFunction=function(){returnthis===window;};this.myMethod=function(){alert(internalFunction());};};varmyObj=newConstructor();myObj.myMethod();这提醒true。为什么内部函数不能将this视为对象?相反,我必须在myMethod中使用alert(internalFunction.call(this));。编辑:我一直

javascript - $(this) 不工作

我正在使用ColorPickerPlugin.我使用以下代码初始化了插件:$(".colorpic").ColorPicker({color:'#0000ff',onShow:function(colpkr){$(colpkr).fadeIn(500);returnfalse;},onHide:function(colpkr){$(colpkr).fadeOut(500);returnfalse;},onChange:function(hsb,hex,rgb){$(this).css('backgroundColor','#'+hex);现在我的问题是$(this)在onchange事