回答編集履歴

3

コメントを受けて追記

2019/06/20 06:15

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア36134

test CHANGED
@@ -157,3 +157,43 @@
157
157
  `new Date()`とした時に呼び出されるのは、普通に`Date`です。**他の何者でもありません**。
158
158
 
159
159
  たしかに、`Date === Date.prototype.constructor`である時は、`Date.prototype.constructor`が呼び出されると考えても間違いではありませんが、それは**たまたま**`Date === Date.prototype.constructor`であった、つまり`Date.prototype.constructor`が`Date`を参照していたから**結果的には間違いではなかった**、という**だけ**のことです。
160
+
161
+
162
+
163
+ ##### コメントを受けて追記
164
+
165
+ > new Date()した際にインスタンスを生成する際最初に必ず走る処理を定義するconstructorが走ると思っています
166
+
167
+
168
+
169
+ それは違います。
170
+
171
+ ```js
172
+
173
+ var f = function(){
174
+
175
+ alert('私はfです')
176
+
177
+ }
178
+
179
+
180
+
181
+ f.prototype.constructor = function(){
182
+
183
+ alert('私はf.prototype.constructorです')
184
+
185
+ }
186
+
187
+
188
+
189
+ f();
190
+
191
+ //私はfです
192
+
193
+
194
+
195
+ new f();
196
+
197
+ //私はfです
198
+
199
+ ```

2

質問の追記にあわせて追記

2019/06/20 06:15

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア36134

test CHANGED
@@ -139,3 +139,21 @@
139
139
  //undefined Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
140
140
 
141
141
  ```
142
+
143
+
144
+
145
+ ##### 質問の追記にあわせて追記
146
+
147
+ > abesiと出てこない事の理由がわかりません
148
+
149
+
150
+
151
+ なるほど……
152
+
153
+ 質問等を拝見して、domidomiさんは「`new Date()`としたときに`Date.prototype.constructor`が呼び出される」と**勘違いされている**のではないかな、と感じました。(違ったらすみません)
154
+
155
+
156
+
157
+ `new Date()`とした時に呼び出されるのは、普通に`Date`です。**他の何者でもありません**。
158
+
159
+ たしかに、`Date === Date.prototype.constructor`である時は、`Date.prototype.constructor`が呼び出されると考えても間違いではありませんが、それは**たまたま**`Date === Date.prototype.constructor`であった、つまり`Date.prototype.constructor`が`Date`を参照していたから**結果的には間違いではなかった**、という**だけ**のことです。

1

コメントを受けて追記

2019/06/20 05:38

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア36134

test CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
  Date.prototype.constructor();
30
30
 
31
- //Tue Jun 18 2019 19:52:00 GMT+0900 (日本標準時) Scratchpad/17:15:11
31
+ //Tue Jun 18 2019 19:52:00 GMT+0900 (日本標準時)
32
32
 
33
33
  //abesi
34
34
 
@@ -59,3 +59,83 @@
59
59
  //Uncaught TypeError: Class constructor Aa cannot be invoked without 'new'
60
60
 
61
61
  ```
62
+
63
+
64
+
65
+ ##### コメントを受けて追記
66
+
67
+ > new Dateを行った際にabesiと表示されないのはなぜでしょうか?
68
+
69
+
70
+
71
+ 違うものだからだと思います。
72
+
73
+ ```js
74
+
75
+ console.log( Date === Date.prototype.constructor );
76
+
77
+ //true
78
+
79
+
80
+
81
+ var itti = Date.prototype.constructor
82
+
83
+ var aaa = function(...args){
84
+
85
+ console.log(itti(...args));
86
+
87
+ console.log('abesi');
88
+
89
+ }
90
+
91
+ Date.prototype.constructor = aaa;
92
+
93
+
94
+
95
+ console.log( Date === Date.prototype.constructor );
96
+
97
+ //false
98
+
99
+ ```
100
+
101
+
102
+
103
+ > 記述の処理を走らせてからDate.prototype.constructor("2003")と引数を入れても現在時刻が帰ってくるのはなぜでしょうか?
104
+
105
+
106
+
107
+ 確認してみましたが、記述の処理を走らせる前からそうでした。
108
+
109
+ きちんとECMAScriptの仕様を調べていませんが、Chrome、Firefox、IE11で同じ動作でした。おそらく仕様ではないかと。
110
+
111
+ ```js
112
+
113
+ console.log( Date.prototype.constructor("2003"), Date("2003") );
114
+
115
+ //Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時) Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
116
+
117
+
118
+
119
+ var itti = Date.prototype.constructor
120
+
121
+ var aaa = function(...args){
122
+
123
+ console.log(itti(...args));
124
+
125
+ console.log('abesi');
126
+
127
+ }
128
+
129
+ Date.prototype.constructor = aaa;
130
+
131
+
132
+
133
+ console.log( Date.prototype.constructor("2003"), Date("2003") );
134
+
135
+ //Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
136
+
137
+ //abesi
138
+
139
+ //undefined Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
140
+
141
+ ```