Cookie Consent by Free Privacy Policy Generator 📌 this in JavaScript.


✅ this in JavaScript.


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

this can be a confusing topic for a lot of us when it comes to JavaScript. And I am going to try to make it a bit more easier of understand and work with this in JavaScript for you.

this

Ok, lets start with this itself. In JavaScript, this is a keyword refers to the context of the code. Now, that context is defined based on what we are calling this and their execution context.

Execution Context refers to an environment in which JavaScript code is executed.

There are several ways to use this, So, let's look at what we get when call this by itself.

console.log(this)

when we run this line of code we get the Window object as output.

Window {window: Window, self: Window, document: document, name: '', location: Location, …}
this window object contains our window methods. We get this as output because in this case context for this is global.

this in function

this when called inside a function it refers to the global object by default even though function creates it's own execution context so,

function this_in_a_function() {
    console.log('this in a function', this);
}

this_in_a_function()

Output:

Window {window: Window, self: Window, document: document, name: '', location: Location, …}

However, if strict mode "use strict" is used it will output undefined.

this in object

When used with object we will see that this will no loger refer to Window Object.

const person ={
    firstName: "John",
    lastName : "Doe",
    id       : 1234,
    getThis : function() {
      return this;
    }
};

console.log(person.getThis());

Output:
Object Output

We can also use this to access object properties and methods.

const person = {
    firstName: "John",
    lastName : "Doe",
    id       : 1234,
    getFullName : function() {
      return this.firstName + ' ' + this.lastName;
    }
};

console.log(person.getFullName());

Output:
Jhon Doe

this in arrow functions

In arrow functions, this is set lexically. That's because the arrow function dose not make it's own execution context but rather inherits it from outer functions.

Generally it will mean that this also refers to Window Object if arrow function is defined in global scope.

const this_in_arrow = () => this

console.log(this_in_arrow())

Output:
Window {window: Window, self: Window, document: document, name: '', location: Location, …}

but a more accurate example would be

function person(){
    name= 'Jhon'
    surname= 'doe'
    return (() => this.name)()

}

console.log(person());

Output:
Jhon

In this example this is scoped to the function person hence the output.

Let's see what happens if we do this same thing with a objects.

const person = {
    firstName: 'Jhon',
    lastName: 'Doe',
    sayName: () => this.firstName
}

console.log(person.sayName());

Output:
undefined

We get undefined. Now, that's because object does not have it's own execution context.

I hope this cleared your doubts about this in JavaScript and helps you in journey to learn JavaScript.

...

✅ JavaScript Game Tutorial – Build a Stick Hero Clone with HTML Canvas + JavaScript


📈 12.91 Punkte

✅ Demo: Objects in JavaScript [44 of 51] | Beginner's Series to JavaScript


📈 12.91 Punkte

✅ 20+ Handy JavaScript Functions to Simplify Your Code | JavaScript Tutorial


📈 12.91 Punkte

✅ JavaScript: Dirty Parts of the Language (aka Maneuvering JavaScript Errors)


📈 12.91 Punkte

✅ How Much JavaScript is Enough JavaScript to Learn React? 🤔


📈 12.91 Punkte

✅ Demo: Data types in JavaScript [16 of 51] | Beginner's Series to JavaScript


📈 12.91 Punkte

✅ Essential ES6 JavaScript Features Every JavaScript Developer Should Know


📈 12.91 Punkte

✅ V8 Internals for JavaScript Developers (Make Your JavaScript Faster)


📈 12.91 Punkte

✅ JavaScript Course in Spanish – Learn JavaScript for Beginners


📈 12.91 Punkte

✅ Mastering JavaScript: Unveiling the Power of DOM Manipulation and the JavaScript Object Model


📈 12.91 Punkte

✅ Javascript - OOP with Javascript


📈 12.91 Punkte

✅ Low CVE-2020-14063: Tc custom javascript project Tc custom javascript


📈 12.91 Punkte

✅ JavaScript console methods for better debugging - 🔮 Unlocking JavaScript Magic


📈 12.91 Punkte

✅ V8 JavaScript engine — Understanding JavaScript API Requests and Responses in the Data Fetching lifecycle


📈 12.91 Punkte

✅ Javascript: Neue Facebook-Webseite setzt komplett auf Javascript


📈 12.91 Punkte

✅ 👨‍💻 Splice method in JavaScript : Unveiling the Magic of JavaScript Arrays


📈 12.91 Punkte

✅ JavaScript Tutorial Series: Introduction to JavaScript events.


📈 12.91 Punkte

✅ Deno : Let's Make JavaScript Uncomplicated. A Powerful NextGen JavaScript Runtime


📈 12.91 Punkte

✅ HTML and Javascript Teacher - Code examples in HTML and Javascript.


📈 12.91 Punkte

✅ JavaScript Game Dev Tutorial – Build Gorillas with HTML Canvas + JavaScript


📈 12.91 Punkte

✅ Introduction to JavaScript: What is JavaScript and Why is it Important for Web Development?


📈 12.91 Punkte

✅ Difference between Asynchronous Javascript and Synchronous Javascript


📈 12.91 Punkte

✅ Top 10 JavaScript Vulnerabilities (aka OWASP Top 10 for JavaScript Developers)


📈 12.91 Punkte

✅ State of JavaScript: Entwicklung und Akzeptanz von JavaScript im Jahr 2022


📈 12.91 Punkte

✅ Dynamic Rendering for JavaScript web apps - JavaScript SEO


📈 12.91 Punkte

✅ How To Create a Calculator Using HTML CSS & JavaScript | Simple Calculator in JavaScript


📈 12.91 Punkte

✅ JavaScript-Experte Crockford: "Wir sollten nicht immer noch JavaScript nutzen"


📈 12.91 Punkte

✅ 10+ Essential JavaScript Functions to Streamline Your Code | JavaScript Guide


📈 12.91 Punkte

✅ Why Is JavaScript So Fast? (aka JavaScript Engines - How Do They Even?)


📈 12.91 Punkte

✅ Mastering JavaScript Screen Capture: Essential Tips for Taking Screenshots | JavaScript Projects


📈 12.91 Punkte

✅ Mozilla Firefox up to 13.0 JavaScript SandBox Utility javascript: URL memory corruption


📈 12.91 Punkte

✅ JavaScript Function , First Class function / First Class Citizen in JavaScript


📈 12.91 Punkte











matomo

Datei nicht gefunden!