[Can'tBeSolved] Problem with .method() declaration using Es6 arrow syntax | SoloLearn: Learn to code for FREE!

+7

[Can'tBeSolved] Problem with .method() declaration using Es6 arrow syntax

I'm getting unexpected output when declaring a custom string method using ES6 Syntax. It is working expected when I use older method i.e. using function keyword, but not with arrow function. Cee my code to see problem. https://code.sololearn.com/W98vFyLcBVRv/?ref=app#js

12/3/2020 10:01:41 AM

Bibek Oli

27 Answers

New Answer

+9

You cannot use the "this" keyword inside an arrow function

+5

Mirielle[ INACTIVE ] Actually, `this` keyword can be used in arrow functions. Arrow functions do not create a new context for `this`. It will use the outer context. So if an arrow function is enclosed in a regular function, it's context is bound to the outer function's scope. The following is appropriate: https://code.sololearn.com/WuO2s2D1yvJX/?ref=app

+5

Ore you cannot use "this" in an arrow function, simple and i wont start the arguement. enclosing it in a regular function isn't an excuse. this = current object you cannot get the current object inside an arrow function with this what you showed reffered to the outer function "fn" not the current function ()=> itself. the prototype of an object from the OP's code reffers to the current object, theres no way on earth to make an arrow function prototype points to this object

+3

https://code.sololearn.com/csXo9FfFBfo9/?ref=app

+3

Btw, I'll now end the discussion and advice you to read more on the "this" keyword.

+3

Good. You have spread enough misinformation already. 1. You cannot use the "this" keyword inside an arrow function 2. you cannot get the current object inside an arrow function with this These are both wrong according to this code. https://code.sololearn.com/WuO2s2D1yvJX 3. the prototype of an object from the OP's code referrs to the current object The prototype is a property of the object that references another object on the heap. It does not refer to the current object. a = {} a !== a.prototype 4. the this keyword represented the object that called the function I don't know what you mean by ‘object that called the function’ but look at this code https://code.sololearn.com/WC9AdODGWMkC

+2

Mirielle[ INACTIVE ] so Saad😔 I was wondering since a long time why I am not being able to do this, finally got the reason. Thanks for the answer.

+2

Mirielle[ INACTIVE ] A better way to express what you are saying is “arrow functions do not have their own context”. As I have proved earlier, `this` can be used in an arrow function. Also, this != current object this = current context

+2

🇮🇳Vivek🇮🇳 read to understand, that's not what we're talking about. "this" in javascript refers to an "object". I don't know what your definition for a "context" means cos that isn't a universal term in javascript. Everything excluding nulls, numbers and Symbols is an object in javascript, and "this" could refer to any of them and when they do so, they refers to an object, you can call it something else as long as they are an object

+2

From w3school site: https://www.w3schools.com/js/js_arrow_function.asp In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever. With arrow functions the this keyword always represents the object that defined the arrow function. ___________________ You may need to read more on this/arrow function so you won't mislead people

+2

Javascript supports OOP right from the beginning, classes was introduced in es6. using {} variable = using an object. also, the window's object is an object

+2

I later made some findings just to be sure if "this" actually refers to a "context" instead of an "object" and I didn't came across any content that verifies that. I'll be sharing more external links including your favorite MDN that explicitly says `this refers to the current object` MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this freecodecamp https://www.freecodecamp.org/news/a-guide-to-this-in-javascript-e3b9daef4df1/ medium https://medium.com/better-programming/understanding-the-this-keyword-in-javascript-cb76d4c7c5e8 jsTutor https://www.javascripttutorial.net/javascript-this/ alligator.io https://alligator.io/js/this-keyword/ reedbarger https://reedbarger.com/why-we-need-this-in-javascript-and-how-to-know-its-value tutorials teacher https://www.tutorialsteacher.com/javascript/this-keyword-in-javascript

+2

Well, I might have confused you by saying this = current context I should have said this = a reference to the object that owns the current execution context That is just a longer way of saying the same thing though. If `this` is the current object, it is expected that `this` remains the same even when a function is bound to another object's context. That does not happen so `this` is not a reference to the current object. The MDN link you shared explains it clearly. """ In most cases, the value of this is determined by how a function is called (runtime binding)... it may be different each time the function is called. ES5 introduced the bind() method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context). """ Is that not clear enough for you?

+1

Mirielle[ INACTIVE ] I suggest read more about `this` from MDN. W3Schools is not a good learning site. David Carroll probably has more to say on that. MDN defines `this` as “A property of an execution context (global, function or eval) that, in non–strict mode, is always a reference to an object and in strict mode can be any value.” https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this See also: https://www.freecodecamp.org/news/how-to-understand-the-keyword-this-and-context-in-javascript-cd624c6b74b8/

+1

In addition, "this" keyword in EVERY Other programming languages refers to the current "OBJECT". Context does not exist as a keyword in programming. In fact, "this" keyword is a syntax of "Object Oriented Programming" not "Context Oriented Programming"

+1

W3schools is one of the best learning site

+1

Mirielle[ INACTIVE ] `this` keyword is different for JavaScript. Partly because it existed way before JavaScript supported OOP. In most languages, `this` refers to the object SCOPE. In JavaScript, `this` refers to the bound CONTEXT. Context is the lexical environment a function is bound to. Scope is the current context of execution. https://code.sololearn.com/WC9AdODGWMkC/?ref=app

+1

I haven't misinformed anyone and that claim will get you blocked after reading this comment. I'll share an external link that clearly says "this" refers to the "CURRENT OBJECT" . I don't know the author and this is to defend your false claim of me misinforming people . to prove yourself right, share an external source that clearly says `this refers to current context` like mine _______________________ https://codeburst.io/all-about-this-and-new-keywords-in-javascript-38039f71780c this keyword refers to an object, that object which is executing the current bit of javascript code. In other words, every javascript function while executing has a reference to its current execution context, called "this". Execution context means here is how the function is called. and function is an object.

+1

Ah cv NLLDJKFTKIglckv vl Torrescjkcklje Don't spam

0

String.bind(this);