8
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

thisをつけないとundefinedが出てしまう件(javascript)

Posted at

苗字と名前をconsoleで出力するクラス(設計図)を定義して
それをインスタンス化(製造)した場合下記のコードだとundefinedが出る

const Member = function (firstName,lastName) {
  firstName = firstName;
  lastName = lastName;
};
const women = new Member('天王寺','璃奈');
console.log(women.firstName +' ' + women.lastName);

スクリーンショット 2020-10-31 14.21.24(3).png

これにthisを付与すると名前を出力してくれる。
これが俗にいうthis.変数を指定する事でインスタンス(製造)のプロパティ(クラスの要素)を設定できるという事。

const Member = function (firstName,lastName) {  
 this.firstName = firstName;
  this.lastName = lastName;
};
const women = new Member('天王寺','璃奈');
console.log(women.firstName +' ' + women.lastName);

スクリーンショット 2020-10-31 14.22.48(3).png

8
11
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?