JavaScript/instanceof:修订间差异
来自康健生活
无编辑摘要 |
|||
| (未显示同一用户的1个中间版本) | |||
| 第6行: | 第6行: | ||
== 继承对象 == | == 继承对象 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
class People{}; | class People{}; | ||
class Student extends People{}; | class Student extends People{}; | ||
| 第16行: | 第16行: | ||
</pre>}}}} | </pre>}}}} | ||
== 原始类型 == | == 原始类型 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
1 instanceof Number; | 1 instanceof Number; | ||
true instanceof Boolean; | true instanceof Boolean; | ||
| 第26行: | 第26行: | ||
</pre>}}}} | </pre>}}}} | ||
== 构造函数 == | == 构造函数 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
new Number(1) instanceof Number; | new Number(1) instanceof Number; | ||
new Boolean(true) instanceof Boolean; | new Boolean(true) instanceof Boolean; | ||
| 第36行: | 第36行: | ||
</pre>}}}} | </pre>}}}} | ||
== 原生类型 == | == 原生类型 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
[1,2,3] instanceof Array; | [1,2,3] instanceof Array; | ||
/abc/ instanceof RegExp; | /abc/ instanceof RegExp; | ||
| 第77行: | 第77行: | ||
</pre>}}}} | </pre>}}}} | ||
== 复杂用法 == | == 复杂用法 == | ||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function Foo(){} | |||
Object instanceof Object; | |||
Function instanceof Function; | |||
Number instanceof Number; | |||
String instanceof String; | |||
Function instanceof Object; | |||
Foo instanceof Function; | |||
Foo instanceof Foo; | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
true | |||
true | |||
false | |||
false | |||
true | |||
true | |||
false | |||
</pre>}}}} | |||
== 继承 == | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function Aoo(){} | |||
function Foo(){} | |||
Foo.prototype = new Aoo(); | |||
var foo = new Foo(); | |||
console.log(foo instanceof Foo); | |||
console.log(foo instanceof Aoo); | |||
</syntaxhighlight>|{{consoleout|<pre>true | |||
true | |||
</pre>}}}} | |||
== 原理 == | == 原理 == | ||