回答編集履歴
3
コメントを受けて追記
answer
CHANGED
@@ -77,4 +77,24 @@
|
|
77
77
|
質問等を拝見して、domidomiさんは「`new Date()`としたときに`Date.prototype.constructor`が呼び出される」と**勘違いされている**のではないかな、と感じました。(違ったらすみません)
|
78
78
|
|
79
79
|
`new Date()`とした時に呼び出されるのは、普通に`Date`です。**他の何者でもありません**。
|
80
|
-
たしかに、`Date === Date.prototype.constructor`である時は、`Date.prototype.constructor`が呼び出されると考えても間違いではありませんが、それは**たまたま**`Date === Date.prototype.constructor`であった、つまり`Date.prototype.constructor`が`Date`を参照していたから**結果的には間違いではなかった**、という**だけ**のことです。
|
80
|
+
たしかに、`Date === Date.prototype.constructor`である時は、`Date.prototype.constructor`が呼び出されると考えても間違いではありませんが、それは**たまたま**`Date === Date.prototype.constructor`であった、つまり`Date.prototype.constructor`が`Date`を参照していたから**結果的には間違いではなかった**、という**だけ**のことです。
|
81
|
+
|
82
|
+
##### コメントを受けて追記
|
83
|
+
> new Date()した際にインスタンスを生成する際最初に必ず走る処理を定義するconstructorが走ると思っています
|
84
|
+
|
85
|
+
それは違います。
|
86
|
+
```js
|
87
|
+
var f = function(){
|
88
|
+
alert('私はfです')
|
89
|
+
}
|
90
|
+
|
91
|
+
f.prototype.constructor = function(){
|
92
|
+
alert('私はf.prototype.constructorです')
|
93
|
+
}
|
94
|
+
|
95
|
+
f();
|
96
|
+
//私はfです
|
97
|
+
|
98
|
+
new f();
|
99
|
+
//私はfです
|
100
|
+
```
|
2
質問の追記にあわせて追記
answer
CHANGED
@@ -68,4 +68,13 @@
|
|
68
68
|
//Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
|
69
69
|
//abesi
|
70
70
|
//undefined Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
|
71
|
-
```
|
71
|
+
```
|
72
|
+
|
73
|
+
##### 質問の追記にあわせて追記
|
74
|
+
> abesiと出てこない事の理由がわかりません
|
75
|
+
|
76
|
+
なるほど……
|
77
|
+
質問等を拝見して、domidomiさんは「`new Date()`としたときに`Date.prototype.constructor`が呼び出される」と**勘違いされている**のではないかな、と感じました。(違ったらすみません)
|
78
|
+
|
79
|
+
`new Date()`とした時に呼び出されるのは、普通に`Date`です。**他の何者でもありません**。
|
80
|
+
たしかに、`Date === Date.prototype.constructor`である時は、`Date.prototype.constructor`が呼び出されると考えても間違いではありませんが、それは**たまたま**`Date === Date.prototype.constructor`であった、つまり`Date.prototype.constructor`が`Date`を参照していたから**結果的には間違いではなかった**、という**だけ**のことです。
|
1
コメントを受けて追記
answer
CHANGED
@@ -13,7 +13,7 @@
|
|
13
13
|
Date.prototype.constructor = aaa;
|
14
14
|
|
15
15
|
Date.prototype.constructor();
|
16
|
-
//Tue Jun 18 2019 19:52:00 GMT+0900 (日本標準時)
|
16
|
+
//Tue Jun 18 2019 19:52:00 GMT+0900 (日本標準時)
|
17
17
|
//abesi
|
18
18
|
```
|
19
19
|
|
@@ -28,4 +28,44 @@
|
|
28
28
|
}
|
29
29
|
Aa();
|
30
30
|
//Uncaught TypeError: Class constructor Aa cannot be invoked without 'new'
|
31
|
+
```
|
32
|
+
|
33
|
+
##### コメントを受けて追記
|
34
|
+
> new Dateを行った際にabesiと表示されないのはなぜでしょうか?
|
35
|
+
|
36
|
+
違うものだからだと思います。
|
37
|
+
```js
|
38
|
+
console.log( Date === Date.prototype.constructor );
|
39
|
+
//true
|
40
|
+
|
41
|
+
var itti = Date.prototype.constructor
|
42
|
+
var aaa = function(...args){
|
43
|
+
console.log(itti(...args));
|
44
|
+
console.log('abesi');
|
45
|
+
}
|
46
|
+
Date.prototype.constructor = aaa;
|
47
|
+
|
48
|
+
console.log( Date === Date.prototype.constructor );
|
49
|
+
//false
|
50
|
+
```
|
51
|
+
|
52
|
+
> 記述の処理を走らせてからDate.prototype.constructor("2003")と引数を入れても現在時刻が帰ってくるのはなぜでしょうか?
|
53
|
+
|
54
|
+
確認してみましたが、記述の処理を走らせる前からそうでした。
|
55
|
+
きちんとECMAScriptの仕様を調べていませんが、Chrome、Firefox、IE11で同じ動作でした。おそらく仕様ではないかと。
|
56
|
+
```js
|
57
|
+
console.log( Date.prototype.constructor("2003"), Date("2003") );
|
58
|
+
//Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時) Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
|
59
|
+
|
60
|
+
var itti = Date.prototype.constructor
|
61
|
+
var aaa = function(...args){
|
62
|
+
console.log(itti(...args));
|
63
|
+
console.log('abesi');
|
64
|
+
}
|
65
|
+
Date.prototype.constructor = aaa;
|
66
|
+
|
67
|
+
console.log( Date.prototype.constructor("2003"), Date("2003") );
|
68
|
+
//Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
|
69
|
+
//abesi
|
70
|
+
//undefined Tue Jun 18 2019 21:08:31 GMT+0900 (日本標準時)
|
31
71
|
```
|