this

Why can't I access `this` within an arrow function? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-19 21:56:46
问题 This question already has answers here : What does “this” refer to in arrow functions in ES6? (7 answers) Closed 4 years ago . This code below should work as expected, and log "meow" , here an example. function Cat () { this.animalNoise = 'meow' } Cat.prototype.sound = () => { console.log(this.animalNoise) } let cat = new Cat() cat.sound() It doesn't work, this error appears TypeError: Cannot read property 'animalNoise' of undefined and when you convert the arrow function to an actual

Why is “this” in an ES6 class not implicit?

喜你入骨 提交于 2019-12-19 17:03:59
问题 I know that ES6 solved a lot of problems that existed with the this keyword in ES5, such as arrow functions and classes. My question relates to the usage of this in the context of an ES6 class and why it has to be written explicitly. I was originally a Java developer and I come from a world where the following lines of code were perfectly natural. class Person { private String myName; public Person() { myName = "Heisenberg"; } public void sayMyName() { System.out.println("My name is " +

Why is “this” in an ES6 class not implicit?

﹥>﹥吖頭↗ 提交于 2019-12-19 16:58:37
问题 I know that ES6 solved a lot of problems that existed with the this keyword in ES5, such as arrow functions and classes. My question relates to the usage of this in the context of an ES6 class and why it has to be written explicitly. I was originally a Java developer and I come from a world where the following lines of code were perfectly natural. class Person { private String myName; public Person() { myName = "Heisenberg"; } public void sayMyName() { System.out.println("My name is " +

C++ this pointer, hidden argument in function calls

孤街醉人 提交于 2019-12-19 11:55:11
问题 I have a stack class with members and a function called push. class STACK { int data; public: void push(int x) { data=x; } } What does C++ do to convert this statement: s1.push(3); to s1.push(this,3); Basically my question is what happens under the hood to generate the this pointer and pass it as a hidden argument? I am actually coding in C. My objective is to have a program which is as close to OOP as possible. For this i have function pointers as members of the structure. Therefore, I want

Why does JavaScript “this” returns different values in Node and Browser environments?

半世苍凉 提交于 2019-12-19 11:29:48
问题 After watching Kyle Simpson's Advanced JavaScript course on Pluralsight, I created a simple code snippet to try out this binding. I usually work in SublimeText editor and have node build engine configured in it and occasionally I run the same code in browser as well. I have noticed one difference in the program output when execute code in Node and in Browser [Chrome]. Following is the code snippet to try out this binding. function foo() { console.log(this.bar); } var bar = "bar1"; var obj =

addEventListener to an element, with the element itself as parameter

一曲冷凌霜 提交于 2019-12-19 10:19:23
问题 First of all, sorry if the title isn't the best one, I couldn't find a better one. How can I set a function as clickEvent for a Div using javascript, and at the same time also set the parameter of the function, to be the Div itself? For example: I have a HTML file that has: <span id='parent'> <div class='child'></div> </span> And a JS file where I have the following code: var el = document.getElementById("parent").getElementsByClassName("child")[0]; el.onclick = someFuntion(this); But the

How do I refer to actual 'this' inside CoffeeScript fat-arrow callback?

别来无恙 提交于 2019-12-19 10:08:56
问题 The title says it all. When I use the fat-arrow in CoffeeScript, it stores this first before calling the function. For example: class myClass constructor: -> element = $ "#id" element.click -> @myMethod(@value) return return myMethod: (c)-> window.console.log(c) return would yield var myClass; myClass = (function() { function myClass() { var element; element = $("#id"); element.click(function() { this.myMethod(this.value); }); return; } myClass.prototype.myMethod = function(c) { window

Can I pass “this” as a parameter to another function in javascript

限于喜欢 提交于 2019-12-19 07:26:42
问题 I have this: $('#slider li').click(function () { var stepClicked = $(this).index(); alert(stepClicked); if (stepClicked != 0) { $('#cs_previous').removeClass('cs_hideMe'); } else { $('#cs_previous').addClass('cs_hideMe'); } $('li.cs_current').removeClass('cs_current'); $($(this)).addClass('cs_current'); moveToNextImage(stepClicked); function moveToNextImage(stepClicked) { alert(stepClicked); var currentIs = $('li.cs_current').index(); var newLeftEdge = currentIs - stepClicked; $('.cs

virtual method table for multiple-inheritance

ぐ巨炮叔叔 提交于 2019-12-19 07:10:12
问题 I'm reading this article "Virtual method table" Example in the above article: class B1 { public: void f0() {} virtual void f1() {} int int_in_b1; }; class B2 { public: virtual void f2() {} int int_in_b2; }; class D : public B1, public B2 { public: void d() {} void f2() {} // override B2::f2() int int_in_d; }; B2 *b2 = new B2(); D *d = new D(); In the article, the author introduces that the memory layout of object d is like this: d: D* d--> +0: pointer to virtual method table of D (for B1) +4:

“var” variables, “this” variables and “global” variables - inside a JavaScript Constructor

六眼飞鱼酱① 提交于 2019-12-19 03:23:51
问题 After my last question, this one is more accurate for me: example: function Foo() { this.bla = 1; var blabla = 10; blablabla = 100; this.getblabla = function () { return blabla; // exposes blabla outside } } foo = new Foo(); what I understand now: this.bla = 1; // will become an attribute of every instance of FOO. var blabla = 10; // will become a local variable of Foo(will **not** become an attribute of every instance of FOO), which could be accessed by any instance of FOO - only if there's