弱弱的问个JS基础的东西 instanceof

zzzaquarius 2010-02-03 09:49:21
var n1 = new Number(4);
var n2 = 4;
n1 instanceof Number //true

n1 instanceof Object //true

n2 instanceof Number //false
n2 instanceof Object //false
var a1 = [];
var a2 = new Array();
a1 instanceof Array //true
a2 instanceof Array //true

为什么数组用字面量和数字用字面量的结果不一样呢

数组字面量定义的数组是Array对象的实例

为什么数字字面量定义的数字就不是Number对象的实例 也不是Object的实例呢
...全文
334 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2010-02-04
  • 打赏
  • 举报
回复
alert(typeof(n1))
alert(typeof(n2))
alert(typeof(a1))
alert(typeof(a2))
街头小贩 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dh20156 的回复:]
The instanceof operator

The production RelationalExpression: RelationalExpression instanceof ShiftExpression isevaluated
as follows:

1.Evaluate RelationalExpression.
2.Call GetValue(Result(1)).
3.Evaluate ShiftExpression.
4.Call GetValue(Result(3)).
5.If Result(4) is not an object,throw a TypeError exception.
6.If Result(4) does not have a [[HasInstance]] method,throw a TypeError exception.
7.Call the [[HasInstance]] method of Result(4) with parameter Result(2).
8.Return Result(7).

[[HasInstance]](V)
Assume F isaFunctionobject.
When the [[HasInstance]] methodof F is called with value V,the following steps are taken:
1.If V is not an object,return false.
2.Call the [[Get]] method of F with property name "prototype".
3.Let O be Result(2).
4.If O is not anobject,throw a TypeError exception.
5.Let V be the value of the [[Prototype]] property of V.
6.If V is null,return false.
7.If O and V refer to the same object or if they refer to objects joined to each other(13.1.2),return true.
8.Go to step 5.
[/Quote]
风兄E文很历害噢
浴火_凤凰 2010-02-04
  • 打赏
  • 举报
回复
不要研究这么细的东西,没有用的
Even713 2010-02-04
  • 打赏
  • 举报
回复
学习了
dh20156 2010-02-04
  • 打赏
  • 举报
回复
The instanceof operator

The production RelationalExpression: RelationalExpression instanceof ShiftExpression isevaluated
as follows:

1.Evaluate RelationalExpression.
2.Call GetValue(Result(1)).
3.Evaluate ShiftExpression.
4.Call GetValue(Result(3)).
5.If Result(4) is not an object,throw a TypeError exception.
6.If Result(4) does not have a [[HasInstance]] method,throw a TypeError exception.
7.Call the [[HasInstance]] method of Result(4) with parameter Result(2).
8.Return Result(7).

[[HasInstance]](V)
Assume F isaFunctionobject.
When the [[HasInstance]] methodof F is called with value V,the following steps are taken:
1.If V is not an object,return false.
2.Call the [[Get]] method of F with property name "prototype".
3.Let O be Result(2).
4.If O is not anobject,throw a TypeError exception.
5.Let V be the value of the [[Prototype]] property of V.
6.If V is null,return false.
7.If O and V refer to the same object or if they refer to objects joined to each other(13.1.2),return true.
8.Go to step 5.
xuetao121 2010-02-04
  • 打赏
  • 举报
回复
var a2 = new Array();
最终的解释也是var a2 = [];
所以a1,a2是一样的。


但是js是弱定义。
var n2 = 4;
不知道是数字,字符串,还是对象。只有你再使用之后才可以根据你使用的情况解释。


我说的解释是编译器的解释
Alien 2010-02-03
  • 打赏
  • 举报
回复
字面量?

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧