JS 7の再学習
11911 ワード
プロトタイプモデルの最適化
//
:Class()
//
JSON
//
1 ,
//
,
//
,
function
Class(){
var
aDefine
=
arguments[arguments.lenght
-
1
];
//
if
(aDefine)
return
;
var
aBase
=
arguments.length
>
1
?
arguments[
0
]:object;
//
function
prototype_(){};
//
prototype ,
var
aPrototype
=
new
prototype_();
//
prototype
for
(
var
member
in
aDefine)
//
prototype
if
(member
!=
"
Create
"
) aPrototype[member]
=
aDefine[member];
if
(aDefine.Create)
//
var
aType
=
aDefine.Create;
//
else
//
aType
=
function
() {
this
.base.apply(
this
,arguments); } aType.prototype
=
aPrototype;
//
( ) prototype
aType.Base
=
aBase;
//
aType.prototype.type
=
aType;
//
Type
return
aType;
//
object
function
object(){};
//
Object ,
object.prototype.isA
=
function
(aType)
//
{
var
self
=
this
.Type;
while
(self) {
if
(self
==
aType)
return
true
; self
=
self.Base; };
return
false
; }; object.prototype.base
=
function
()
//
{
var
Caller
=
object.prototype.base.caller; Caller
&&
Caller.Base
&&
Claller.Base.apply(
this
, arguments); };
var
Person
=
Class
//
object
({ Create:
function
(name,age) {
this
.base();
this
.name
=
name;
this
.age
=
age; }, SayHello:
function
() { alert(
"
my name is
"
+
this
.name
+
"
my age is
"
+
this
.age); } });
var
Employee
=
Class ({ Create:
function
(name,age,salary) {
this
.base(name,age);
//
this
.salary
=
salary; }, ShowMeTheMoney:
function
() { alert(
this
.name
+
"
RMB
"
+
this
.salary); } });
var
bill
=
new
Person(
"
bill
"
,
53
);
var
steve
=
new
Employee(
"
steve
"
,
53
,
1234
); bill.SayHello(); steve.SayHello(); steve.ShowMeTheMoney();
var
littleBill
=
new
bill.Type(
"
little bill
"
,
6
); littleBill.SayHello(); alert(bill.isA(Person));
//
true
alert(bill.isA(Employee));
//
false
alert(steve.isA(Person));
//
true
}