「JavaScript/演算子」の版間の差分

削除された内容 追加された内容
Ef3 (トーク | 投稿記録)
Ef3 (トーク | 投稿記録)
→‎new演算子: 例など追加
587 行
 
== new演算子 ==
new 演算子は、ユーザー定義のオブジェクト型や、コンストラクタ機能を持つ組み込みオブジェクト型のインスタンスを作成します<ref>[https://tc39.es/ecma262/#sec-new-operator ECMA-262::13.3.5 The new Operator]</ref>。
<syntaxhighlight lang="js"> new コンストラクタ </syntaxhighlight>
 
=== new演算子の構文 ===
<syntaxhighlight lang="js"> new コンストラクタ [ ( [ パラメータ列 ] ) ]</syntaxhighlight>
 
'''例'''
<syntaxhighlight lang="js">
/*
* function 製のコンストラクタに適用した例
*/
const T = function(x = 0) {
this.x = x;
}
T.prototype.value = function() {
return this.x;
}
 
const t = new T(42);
console.log(t.value())
 
/*
* class に適用した例
*/
class C {
constructor(x = 0) {
this.x = x;
}
value() {
return this.x;
}
}
 
const c = new C(13);
console.log(c.value())
</syntaxhighlight>
 
== void 演算子 ==