JavaScript OOP(二) -- 自定义Object
1.入门--构造函数
function Student(name, age) {
this.name = name;
this.age = age;
this.toString = function() {
return this.name + " : " + this.age;
}
}
var stu = new Student("siyuan", 24);
函数Studnet此时的充当的角色有:
1)类型声明
2)类型实现
3)类型引用
4)构造函数
2.动态add和delete属性
1)add
stu.id = 567;
2)delete
delete stu.id;
--delete清除window对象,系统对象,用户对象等的"用户声明属性",但不能清除如prototype,constructor这样的系统属性。
--此外,delete也可以清除数组中的元素(但不会因为清除元素而使数组长度发生变化)
3.进阶--构造函数的prototype属性
function CollegeStudent(major) {
this.major = major;
this.toString = function() {
return this.name + " : " + this.age + " : " + this.id + " : " + this.major;
}
}
CollegeStudent.prototype = new Student("siyuan", 24);
CollegeStudent.prototype.id = 9999;
4.instanceof
列举整个prototype链以检测类型
1)number/boolean/string/undefined instanceof Object == false (值类型变量)
2)function/object instanceof Object == true (引用类型变量)
3)null instanceof Object == false
4)其它对象instanceof Object均为true (引用类型变量)
5.with/this
this关键字的用法
1)在构造函数中,指代新创建的对象实例
2)在对象的方法被调用时,指代调用该方法的对象实例
注:如果一个函数被作为普通函数(fun(...)的形式)调用,那么函数的this关键字指向window对象;
如果this关键字不在任何函数中,那么它也指向window对象。
apply([thisObj[,argArray]])和call([thisObj[,arg1[, arg2[, [,.argN]]]]])函数
应用某一对象的一个方法,用另一个对象替换当前对象。
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。
两个函数功能类似,区别在于arguments的传递方式
with语句
为语句默认对象
如果不使用with()语法,那么这段代码将要受到更外层的with()语句的影响;
如果没有更外层的with(),那么这段代码的“默认使用的对象实例”将是window。
注:this与with关键字不是互为影响的(with语句只限定对obj的既有属性的读取,而不能主动的声明它)。
6.for(prop in object)/for(element in array)
for循环,用于迭代数组元素或者对象属性
在循环的每次迭代前,variable 被赋予 object 的下一个属性(不区分属性和方法,不包含Object的属性和方法)或 array 的下一个元素。然后可以在循环内的任一语句中使用它,就好像正在使用 object 的该属性或 array 的该元素一样。
当在一个对象上迭代时,没有办法决定或控制把对象的成员赋给 variable 的次序。在数组内将按元素的次序执行迭代,也就是,0、1、2、......
7.constructor
对象属性,指向对象构造方法或者对象构造方法.prototype.对象构造方法(优先)
为了解决对象与对象构造方法不一致的问题,经常在对构造方法.prototype赋值后进行下述操作
构造方法.prototype.constructor = 构造方法
8.new过程
function new(constructorFun) {
var _this = {};
var _prot = constructorFun.prototype;
_this.getter = function(attrName) {
if(attrName in _this)
return _this[attrName];
else if(attrName in _prot)
return _prot[attrName];
else
return undefined;
}
_this.setter = function(attrName, value) {
_this[attrName] = value;
}
if (_prot && !_prot.constructor)
_prot.constructor = constructorFun;
_this.constructor = _prot ? _prot.constructor : constructorFun;
constructorFun.call(_this);
return _this;
}
9.Garbage Collect垃圾回收机制
对象在失效的情况下可能会被垃圾回收机制释放,对象失效的情况有:
1)对象在其生存的上下文环境之外
2)一个全局变量没有被引用
注:对象的失效并不等于会释放
<html>
<head>
<title>object test</title>
<script language="javascript">
<!--
var stu;
function Student(name, age) {
this.name = name;
this.age = age;
this.sayHello = function() {
alert("Hello, I am " + this.name);
}
this.toString = function() {
return this.name + " : " + this.age;
}
}
function testNew() {
stu = new Student("siyuan", 24);
alert(stu);
}
function testAddAttr() {
alert("id = " + stu.id);
alert("dynamic add attribute id");
stu.id = "567";
alert("id = " + stu.id);
}
function testDelete() {
alert("id = " + stu.id);
alert("dynamic delete attribute id");
delete stu.id;
alert("id = " + stu.id);
}
function CollegeStudent(major) {
this.major = major;
this.toString = function() {
return this.name + " : " + this.age + " : " + this.id + " : " + this.major;
}
}
var studentPrototype = new Student("siyuan", 24);
CollegeStudent.prototype = studentPrototype;
CollegeStudent.prototype.id = 9999;
var colleger;
function testPrototype() {
colleger = new CollegeStudent("software engineer");
alert(colleger);
}
function testForIn() {
var student = new Student("siyuan", 24);
for (prop in student) {
alert(prop + " : " + student[prop]);
}
}
function testForInArray() {
var array1 = new Array("1", "2", "3");
for (prop in array1) {
alert(prop + " : " + array1[prop]);
}
}
function foo() {
alert(this.name);
}
function testApply() {
foo();
var student = new Student("siyuan", 24);
foo.apply(student);
foo.apply();
}
function testCall() {
foo();
var student = new Student("siyuan", 24);
foo.call(student);
foo.call();
}
function forTestWith(obj) {
with(obj) {
id = "1";
name = "testName1";
// this.name = "testName";
}
}
function testWith() {
var obj = { id : "2" };
forTestWith(obj);
alert(obj.name + " : " + obj.id);
alert(window.name + " : " + window.id);
}
function testInstanceof(){
alert("basic types")
alert(1 instanceof Object);
alert("1" instanceof Object);
alert(false instanceof Object);
alert(undefined instanceof Object);
alert(null instanceof Object);
alert(testInstanceof instanceof Object);
var stu = new Student("siyuan", 24);
alert(stu);
alert(stu instanceof Student);
alert(stu instanceof CollegeStudent);
alert(stu instanceof Object);
var collegeStu = new CollegeStudent("software engineer");
alert(collegeStu);
alert(collegeStu instanceof Student);
alert(collegeStu instanceof CollegeStudent);
alert(collegeStu instanceof Object);
var temp = { name : "siyuan" };
alert(temp.name);
alert(temp instanceof Object);
}
function SuperCollegeStudent(title) {
// alert(this.constructor);
this.title = title;
}
var collegeStudentPrototype = new CollegeStudent("software engineer");
SuperCollegeStudent.prototype = collegeStudentPrototype;
SuperCollegeStudent.prototype.constructor = SuperCollegeStudent;
function testConstructor() {
var stu = new Student("siyuan", 24);
alert(stu.constructor);
//alert(stu.prototype);
var collegeStu = new CollegeStudent("software engineer");
alert(collegeStu.constructor);
var superCollegeStu = new SuperCollegeStudent("sofrware develop flow");
alert(superCollegeStu.constructor);
}
function testNew() {
var collegeStu = new CollegeStudent("software engineer");
//studentPrototype.name = "siyuan1";
alert(collegeStu);
}
-->
</script>
</head>
<body>
<input type="button" value="new Student" onclick="javascript:testNew();"/><br>
<input type="button" value="add attribute id" onclick="javascript:testAddAttr();"/><br>
<input type="button" value="delete attribute id" onclick="javascript:testDelete();"/><br>
<input type="button" value="test prototype" onclick="javascript:testPrototype();"/><br>
<input type="button" value="for ... in ... object" onclick="javascript:testForIn();"/><br>
<input type="button" value="for ... in ... array" onclick="javascript:testForInArray();"/><br>
<input type="button" value="apply" onclick="javascript:testApply();"/><br>
<input type="button" value="call" onclick="javascript:testCall();"/><br>
<input type="button" value="with" onclick="javascript:testWith();"/><br>
<input type="button" value="instanceof" onclick="javascript:testInstanceof();"/><br>
<input type="button" value="constructor" onclick="javascript:testConstructor();"/><br>
<input type="button" value="new" onclick="javascript:testNew();"/><br>
</body>
</html>