hookee 刚才结贴过快,soryy

plglenn35 2010-01-03 01:29:01
function New(aClass, aParams) //创建对象的函数,用于任意类的对象创建
{
function new_() //创建对象的临时函数壳
{
this.Type = aClass; //我们也给每一个对象约定一个Type属性,据此可以访问到对象所属的类
if (aClass.Create)
aClass.Create.apply(this, aParams); //我们约定所有类的构造函数都叫Create,这和DELPHI比较相似
};
new_.prototype = aClass; //这个用法奇怪啊
return new new_();
};

背景


//语法甘露:
var object = //定义小写的object基本类,用于实现最基础的方法等
{
isA: function(aType) //一个判断类与类之间以及对象与类之间关系的基础方法
{
var self = this;
while(self)
{
if (self == aType)
return true;
self = self.Type;
};
return false;
}
};

function Class(aBaseClass, aClassDefine) //创建类的函数,用于声明类及继承关系
{
function class_() //创建类的临时函数壳
{
this.Type = aBaseClass; //我们给每一个类约定一个Type属性,引用其继承的类
for(var member in aClassDefine)
this[member] = aClassDefine[member]; //复制类的全部定义到当前创建的类
};
class_.prototype = aBaseClass;
return new class_();
};

function New(aClass, aParams) //创建对象的函数,用于任意类的对象创建
{
function new_() //创建对象的临时函数壳
{
this.Type = aClass; //我们也给每一个对象约定一个Type属性,据此可以访问到对象所属的类
if (aClass.Create)
aClass.Create.apply(this, aParams); //我们约定所有类的构造函数都叫Create,这和DELPHI比较相似
};
new_.prototype = aClass;
return new new_();
};

//语法甘露的应用效果:
var Person = Class(object, //派生至object基本类
{
Create: function(name, age)
{
this.name = name;
this.age = age;
},
SayHello: function()
{
alert("Hello, I'm " + this.name + ", " + this.age + " years old.");
}
});

var Employee = Class(Person, //派生至Person类,是不是和一般对象语言很相似?
{
Create: function(name, age, salary)
{
Person.Create.call(this, name, age); //调用基类的构造函数
this.salary = salary;
},
ShowMeTheMoney: function()
{
alert(this.name + " $" + this.salary);
}
});

var BillGates = New(Person, ["Bill Gates", 53]);
var SteveJobs = New(Employee, ["Steve Jobs", 53, 1234]);
BillGates.SayHello();
SteveJobs.SayHello();
SteveJobs.ShowMeTheMoney();

var LittleBill = New(BillGates.Type, ["Little Bill", 6]); //根据BillGate的类型创建LittleBill
LittleBill.SayHello();

alert(BillGates.isA(Person)); //true
alert(BillGates.isA(Employee)); //false
alert(SteveJobs.isA(Person)); //true
alert(Person.isA(Employee)); //false
alert(Employee.isA(Person)); //true
...全文
162 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2010-01-03
  • 打赏
  • 举报
回复
var c=new b();
alert(c.p);// 1
c.p=2;
alert(c.p);// 2

之所以 var c=new b();
alert(c.p);// 1

就是因为 prototype 链的原因

c 因为是 function 声明的对象所以有 prototype 属性

c 没有 p 属性,但 会去找c 的隐含 prototype 属性,是引用值.

就找到了 b ,因为b 有p 属性,所以就返回了 b.p 也就是 1

当c 有p 属性时 就覆盖了 父类 (可以这样理解) 的 p 属性


xuyiazl 2010-01-03
  • 打赏
  • 举报
回复
继续路过~~~
阿非 2010-01-03
  • 打赏
  • 举报
回复

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<script>
var b=function() {
}
b.prototype.p=1;

var c=new b();
alert(c.p);
c.p=2;
alert(c.p);
</script>

</BODY>
</HTML>

plglenn36 2010-01-03
  • 打赏
  • 举报
回复
prototype

我大概懂了一点
阿非 2010-01-03
  • 打赏
  • 举报
回复
依靠 prototype 来实现 继承的效果
阿非 2010-01-03
  • 打赏
  • 举报
回复
是 prototype 链

87,955

社区成员

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

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