JS this
this
1.javascriptには、基本的にコンテキストをグローバルオブジェクトにバインドするルールがあります.
2.ES 6から利用可能な矢印関数(arrow function)は、既存のコンテキストバインド規則に従わない.実行時に、バインドルールが既存のスタックに適用されます.「ダイナミックバインド」とも言えます.
3.現在の実行コードの実行コンテキスト
📋 オブジェクトメソッドリファレンスメソッドの所有者。
単独で使用する場合、所有者はグローバルオブジェクトであるため、グローバルオブジェクトを参照します.coworkers.showAll = function() {
for (var key in coworkers) {
document.write(key + ' : ' + coworkers[key] + '<br>');
}
}
coopers変数名を変更した場合は、関数を変更する必要があるため、上記の方法はあまりよくありません.coworkers.showAll = function() {
for (var key in this) {
document.write(key + ' : ' + this[key] + '<br>');
}
}
同僚が方法で書いたオブジェクトを指す代わりにthisを使用します.
📋 Dot Notation
personにキーと価値を与えた後.ストーブ値へのアクセス方法
インジケータポイントの前のオブジェクト自体(person).
PersonオブジェクトはfullNameメソッドの所有者です
let id = 9484;
let firstName = "Chris";
const person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
person.fullName(); // John Doe
🧹 もう1つの例let number = 55;
foo = () => {
let number = 77;
bar(number)
}
bar = () => {
console.log(this.number);
}
foo(); // 55
bar関数のこれ.numberはグローバル変数number=55を使用するため、fooのbarにはグローバル変数が含まれます.
📋 ブラウザウィンドウでは、グローバルオブジェクトはObject Windowです。
let x = this;
// x = window
📋 call(), apply()
この値をあるコンテキストから別のコンテキストに移動します.
🧹 例1// call 또는 apply의 첫 번째 인자로 객체가 전달될 수 있으며 this가 그 객체에 묶임
var obj = {a: 'Custom'};
// 변수를 선언하고 변수에 프로퍼티로 전역 window를 할당
var a = 'Global';
function whatsThis() {
return this.a; // 함수 호출 방식에 따라 값이 달라짐
}
whatsThis(); // this는 'Global'. 함수 내에서 설정되지 않았으므로 global/window 객체로 초기값을 설정한다.
whatsThis.call(obj); // this는 'Custom'. 함수 내에서 obj로 설정한다.
whatsThis.apply(obj); // this는 'Custom'. 함수 내에서 obj로 설정한다.
🧹 例2function add(c, d) {
return this.a + this.b + c + d;
}
var o = {a: 1, b: 3};
// 첫 번째 인자는 'this'로 사용할 객체이고 (var o로 부터 가져오는),
// 이어지는 인자들은 함수 호출에서 인수로 전달된다.
add.call(o, 5, 7); // 16
// 첫 번째 인자는 'this'로 사용할 객체이고 (var o로 부터 가져오는),
// 두 번째 인자는 함수 호출에서 인수로 사용될 멤버들이 위치한 배열이다.
add.apply(o, [10, 20]); // 34
📋 newの関数の使用
newをコンストラクション関数として使用すると、このオブジェクトは空のオブジェクトになります.function Person () {
console.log(this);
}
new Person();
このとき、thisは空のオブジェクトを指し、コンストラクション関数はthisという空のオブジェクトを返します.function Person () {
console.log(this.age); // undefined
this.age = 100;
console.log(this.age); // 100
}
new Person();
person関数がnewとともにコンストラクション関数として使用されると、関数内部thisは空のオブジェクトになります.this.age = 100;
空のオブジェクトにageというプロパティを追加し、値100を指定します.
次に、person関数はage:100というオブジェクトを返します.class Car {
constructor(a, b) {
this.name = a;
this.price = b;
}
}
const myCar = new Car("장난감", 1000);
注意:
https://www.boostcourse.org/cs124/lecture/194641?isDesc=false
https://www.w3schools.com/js/js_this.asp
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/this
https://im-developer.tistory.com/96
Reference
この問題について(JS this), 我々は、より多くの情報をここで見つけました
https://velog.io/@wjdghks963/JS-this
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
coworkers.showAll = function() {
for (var key in coworkers) {
document.write(key + ' : ' + coworkers[key] + '<br>');
}
}
coworkers.showAll = function() {
for (var key in this) {
document.write(key + ' : ' + this[key] + '<br>');
}
}
let id = 9484;
let firstName = "Chris";
const person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
person.fullName(); // John Doe
let number = 55;
foo = () => {
let number = 77;
bar(number)
}
bar = () => {
console.log(this.number);
}
foo(); // 55
let x = this;
// x = window
// call 또는 apply의 첫 번째 인자로 객체가 전달될 수 있으며 this가 그 객체에 묶임
var obj = {a: 'Custom'};
// 변수를 선언하고 변수에 프로퍼티로 전역 window를 할당
var a = 'Global';
function whatsThis() {
return this.a; // 함수 호출 방식에 따라 값이 달라짐
}
whatsThis(); // this는 'Global'. 함수 내에서 설정되지 않았으므로 global/window 객체로 초기값을 설정한다.
whatsThis.call(obj); // this는 'Custom'. 함수 내에서 obj로 설정한다.
whatsThis.apply(obj); // this는 'Custom'. 함수 내에서 obj로 설정한다.
function add(c, d) {
return this.a + this.b + c + d;
}
var o = {a: 1, b: 3};
// 첫 번째 인자는 'this'로 사용할 객체이고 (var o로 부터 가져오는),
// 이어지는 인자들은 함수 호출에서 인수로 전달된다.
add.call(o, 5, 7); // 16
// 첫 번째 인자는 'this'로 사용할 객체이고 (var o로 부터 가져오는),
// 두 번째 인자는 함수 호출에서 인수로 사용될 멤버들이 위치한 배열이다.
add.apply(o, [10, 20]); // 34
function Person () {
console.log(this);
}
new Person();
function Person () {
console.log(this.age); // undefined
this.age = 100;
console.log(this.age); // 100
}
new Person();
this.age = 100;
class Car {
constructor(a, b) {
this.name = a;
this.price = b;
}
}
const myCar = new Car("장난감", 1000);
Reference
この問題について(JS this), 我々は、より多くの情報をここで見つけました https://velog.io/@wjdghks963/JS-thisテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol