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的实例呢
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的实例呢
alert(typeof(n2))
alert(typeof(a1))
alert(typeof(a2))
最终的解释也是var a2 = [];
所以a1,a2是一样的。
但是js是弱定义。
var n2 = 4;
不知道是数字,字符串,还是对象。只有你再使用之后才可以根据你使用的情况解释。
我说的解释是编译器的解释
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.