1.原始值与引用值
原始值存放在栈里, 引用值存放在堆里. 如程序:
代码如下:
function Person(id,name,age){
this.id = id;
this.name = name;
this.age = age;
}
var num = 10;
var bol = true;
var str = "abc";
var obj = new Object();
var arr = ['a','b','c'];
var person = new Person(100,"笨蛋的座右铭",25);
2.undefined和null
undefined: 变量未定义; 是Undefined类型的专属值;
null:引用未分配; 是Null类型的专属值.
typeof(undefined) == undefined;
typeof(null) == object;
undefined==null;
undefined!==null;
null instanceof Object == false;
undefined instanceof Object == false;
虽然有Undefined和Null类型, 但是通过下面的例子说明这两个类型是不可见的, 也就是说我们只能使用他们的值:
alert(undefined instanceof Undefined);
alert(null instanceof Null);
3.伪数组
特点:
1) 具有length属性;
2) 像数组一样按索引顺序存取数据;
3) 不具备数组特有的操作数据的方法如push, pop, slice...
伪数组都可以通过Array.prototype.slice转换为真正的数组:
var faceArray = {0: 'a', 1: 'b', length: 2}//标准的伪数组;
var realArray = Array.prototype.slice.call(fakeArray);
js中的伪数组:arguments, node.childNodes, document.getElementsByTagName()...
IE中的问题 : IE中node.childNodes是不能用slice转化的.
Jquery中的伪数组 : Jquery本身就是一个伪数组:
alert($('.class1').length); alert($('.class1').[0].tagName);
4.关于简单类型的字面量
var a = 1; b = true, c = "ccc";
字面量看起来有类型
alert(typeof a);//number
alert(typeof b);//boolean
alert(typeof c);//string
但是通过instanceof却测不出来
alert(a instanceof Number)//false
alert(a instanceof Object)//false
alert(b instanceof Boolean)//false
alert(b instanceof Object)//false
alert(c instanceof String)//false
alert(c instanceof Object)//false
5.函数的prototype属性和对象实例的内部prototype属性
每个function(构造函数)都有一个prototype属性, 每个对象实例都有一个不可见的(mozilla把它公开了, 可以通过__proto__来取得)内部的prototype属性, 它指向构造函数的prototype属性. prototype还可以有它自己的prototype属性, 这构成了prototype链, Object是最顶的对象, 所以所有的prototype链最终会指向Object.prototype. 当访问对象实例的属性/方法的时候, 从对象实例自己开始搜索, 若果搜索不到, 沿着prototype链向上搜索, 直到Object.prototype.prototype == null 为止.
6.构造函数的一个小秘密
代码如下:
var s = new function(){return "sss"};
新闻热点
疑难解答
图片精选