JavaScript applyメソッドの使い方

環境
Google Chrome 109.0.5414.120
Windows 10 Home 64bit

書式
func.apply(thisArg, [ argsArray])
thisArg
this の値で、 func の呼び出しで提供されます。
指定した this と引数で関数を呼び出した結果が返ります。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const obj = {
str: 'Study Skill'
}
function funA(){
console.log(this.str);
}
funA.apply(obj);
const obj = { str: 'Study Skill' } function funA(){ console.log(this.str); } funA.apply(obj);
const obj = {
    str: 'Study Skill'
}

function funA(){
    console.log(this.str);
}

funA.apply(obj);

実行結果
> “Study Skill"

JavaScript

Posted by arkgame