this

babel 编译后 this 变成了 undefined

谁都会走 提交于 2020-01-07 05:57:42
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最近有在用webpack,使用了babel这个模块来编译js jsx文件,但是发现文件编译后this变成了undefined。 源文件 module.exports = React.createClass({ render: () => { return (<div>{this.props.name}</div>); } }); 编译后 module.exports = React.createClass({ render: () => { return (<div>{undefined.props.name}</div>); } }); 这个原因是因为webpack规则中babel用了es2015的编译规则 { test: /\.jsx?$/, loader: "babel", query: { presets: ['react', 'es2015'] } } 解决的方法是这样修改 module.exports = React.createClass({ render: function(){ return (<div>{this.props.name}</div>); } }); 其根本的原因是es2015的箭头函数,这个一个不能忽视的问题 普通function函数和箭头函数的行为有一个微妙的区别

How to access the correct `this` inside a callback?

☆樱花仙子☆ 提交于 2020-01-07 05:07:50
问题 I have a constructor function which registers an event handler: function MyConstructor(data, transport) { this.data = data; transport.on('data', function () { alert(this.data); }); } // Mock transport object var transport = { on: function(event, callback) { setTimeout(callback, 1000); } }; // called as var obj = new MyConstructor('foo', transport); However, I'm not able to access the data property of the created object inside the callback. It looks like this does not refer to the object that

Modify this change function to use (this) and the next element with same name as the id of (this)

扶醉桌前 提交于 2020-01-07 02:44:06
问题 This Dropkick change function updates the value of another <select> element when an option is selected. I'm using the function on 300+ pairs of <select> elements so I'd love to modify the function so as not to require specifying the selectors for each element. Since the elements aren't positioned next to each other, I'd like to incorporate this method for getting the next element with same name as the id of the selected input stackoverflow.com/a/7734190/1056713 EDIT - In order to simplify the

Assign 'this' reference to a variable in javascript

送分小仙女□ 提交于 2020-01-07 02:16:41
问题 Is it possible to assign 'this' reference to a variable in javascript. What I want is: var someVariable = this; alert(someVariable); But I'm getting alert saying 'undefined'. 回答1: Yes, it is possible to assign this to a variable. When you get undefined , then you probably use/alert the variable outside the scope where it was defined. This will work: var someVariable; function someFunction () { someVariable = this; } new someFunction(); alert(someVariable); 来源: https://stackoverflow.com

Assign 'this' reference to a variable in javascript

偶尔善良 提交于 2020-01-07 02:16:10
问题 Is it possible to assign 'this' reference to a variable in javascript. What I want is: var someVariable = this; alert(someVariable); But I'm getting alert saying 'undefined'. 回答1: Yes, it is possible to assign this to a variable. When you get undefined , then you probably use/alert the variable outside the scope where it was defined. This will work: var someVariable; function someFunction () { someVariable = this; } new someFunction(); alert(someVariable); 来源: https://stackoverflow.com

The 'this' keyword in functions

拜拜、爱过 提交于 2020-01-06 23:57:19
问题 Taken from ejohn.org: function katana(){ this.isSharp = true; } katana(); assert( isSharp === true, "A global object now exists with that name and value." ); This comes out as true. Could anyone explain this? Inside the function we see this.isSharp = true , doesn't that create an object which should have the propery isSharp , and its value would be true ? (I would think the object is katana, since it calls the function, so katana.isSharp would be true ). In other words, what exactly does the

How to set object property in prototype function (scope problem)?

 ̄綄美尐妖づ 提交于 2020-01-06 15:26:09
问题 This is something trivial, which I've forgotten. There are possibly duplicates - I searched a little, found similar, but couldn't find as concise. String.prototype.test = function(){this.bar = this.length + 2;} var str = "foo"; str.test(); console.log(str); // foo console.log(str.bar); // undefined Pretty sure it has to do with this being trapped in the closure. 回答1: Has to do with the way you're creating your string in this case. Try: var str = new String("Foo"); and you'll find it magically

javascript 'this' scope in jQuery

痞子三分冷 提交于 2020-01-06 08:46:13
问题 I have just converted an piece of code that was an object literal to a class and am having problems with the scope in a jQuery $.each() loop. Say I have a class... var myClass = function(var1) { this.var1 = var1; } myClass.prototype.myFuncion = function() { var context = this; $.each(this.var1, function() { context.myOtherFunction() //is there any way of accessing 'this' from here? }) } I want to know how to access the class context from within the each? I know I can define a variable outside

Javascript Class, this is undefined when returning Promise [duplicate]

社会主义新天地 提交于 2020-01-06 06:51:23
问题 This question already has answers here : Getting this as undefined when using arrow function (1 answer) React 'this' is undefined in Chrome developer tools [duplicate] (2 answers) Visual Studio shows wrong value for `this` in TypeScript [duplicate] (2 answers) Closed 4 months ago . I have a typescript app that it is losing 'this' context inside a class. This class is responsible to setup new Express server instance. I've setup a mock of the code to explain where I'm losing 'this' context. In

Identify Reference to Destroy & Re-Initialize Swiper in Elementor

对着背影说爱祢 提交于 2020-01-06 04:30:06
问题 Elementor Pro (the WordPress page builder) integrates beautifully with Swiper, tying their GUI to the JS parameters and database content. However, for my project, I need to make some changes to the Swiper "CoverFlow" skin Init parameters (show more slides, change the 3D effect facing direction...). My hope is to to use the Destroy method of the Swiper API which looks like: mySwiper.destroy(deleteInstance, cleanStyles); Then I can initialize the Swiper again, with my own custom parameters. The