“JavaScript ini” Kode Jawaban

Ini JavaScript

//this refer to global object or window object
//but when we call this inside method of object it refers to current object
console.log(this===window)//true
let user = {
  name: "Shirshak",
  age: 25,

  sayHi() {
    // "this" is the "current object"
    console.log(this.name); //print shirshak
  }
};
Shirshak kandel

ini js

let user = {
  name: "John",
  age: 30,

  sayHi() {
    // "this" is the "current object"
    alert(this.name);
  }

};

user.sayHi(); // John
shahul

Kata kunci ini di JavaScript

// this keyword

// This keyword belongs to the object it belongs to
// (1).Alone, this refers to the global object.
// (2).In a regular function,this refers to the global object.
// (3).In a method, this refers to the owner object.

// 1
console.log(this);

// 2
function abc() {
  console.log(this);
}
abc();

// 3
const obj = {
  name: "Abhishek",
  no: 1,
  sum: function (a, b) {
    console.log("hello sum", a + b);
    console.log("this:::", this);
    console.log("this name:::", this.name);
  },
};

obj.sum(4, 3);
Abhishek

JavaScript ini

/*In general, the 'this' references the object of which the function is a property.
In other words, the 'this' references the object that is currently calling the function.
Suppose you have an object called 'counter' that has a method 'next()'.
When you call the 'next()' method, you can access the this object. */

let counter = {
  count: 0,
  next: function () {
    return ++this.count;
  },
};
counter.next(); 
//Inside the next() function, the this references the counter
Code language: JavaScript (javascript)
enochanoak

Javascript Kata kunci ini

const person = {
    name: 'John',
    age: 30,

    // accessing name property by using this.name
    greet: function() { console.log('The name is' + ' ' + this.name); }
};

person.greet();
SAMER SAEID

JavaScript ini

// Im Webbrowser ist das window Objekt das globale Objekt:
console.log(this === window); // true

a = 37;
console.log(window.a); // 37

this.b = "MDN";
console.log(window.b);  // "MDN"
console.log(b);         // "MDN"
Obedient Oryx

Jawaban yang mirip dengan “JavaScript ini”

Pertanyaan yang mirip dengan “JavaScript ini”

Lebih banyak jawaban terkait untuk “JavaScript ini” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya