回答編集履歴

1

追記

2019/09/20 07:15

投稿

maisumakun
maisumakun

スコア145208

test CHANGED
@@ -3,3 +3,39 @@
3
3
 
4
4
 
5
5
  なお、`new`の有無で関数の動作を変えたい場合、`this instanceof myFunc`のような方法でチェックするのが確実です。
6
+
7
+
8
+
9
+ ----
10
+
11
+
12
+
13
+ (追記)
14
+
15
+
16
+
17
+ > console.log(obj1)の出力がmyFunc {prop1: "bar"}にする場合はどのような記述になるのでしょうか?
18
+
19
+
20
+
21
+ 上に書いたように、`instanceof`で振り分けられます。
22
+
23
+
24
+
25
+ ```javascript
26
+
27
+ function myFunc() {
28
+
29
+ if(!(this instanceof myFunc)) return 'boo';
30
+
31
+ this.prop1 = 'bar';
32
+
33
+ }
34
+
35
+
36
+
37
+ const obj1 = new myFunc();
38
+
39
+ const obj2 = myFunc();
40
+
41
+ ```