<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
function inherit(p){
if(p==null) throw TypeError();
if(Object.create) return Object.create(p);
var t=typeof p;
if(t!=="object"&& t!=="function") throw TypeError();
function f(){};
f.prototype=p;
return new f();
}
function enumeration(namesToValues){
var enumeration=function(){throw "can't instantiate enumeration"};
var proto=enumeration.prototype={
constructor:enumeration,
toString:function(){return this.name;},
valueOf:function(){return this.value;},
toJSON:function(){return this.name;}
};
enumeration.values=[];
for(name in namesToValues){
var e=inherit(proto);
e.name=name;
e.value=namesToValues[name];
enumeration[name]=e;
enumeration.values.push(e);
}
enumeration.foreach=function(f,c){
for (var i=0;i<this.values.length;i++)
f.call(c,this.values[i]);
};
return enumeration;//
}
var Coin=enumeration({Penny:1,Nickel:5,Dime:10,Quarter:25});
var c=Coin.Dime;
console.log(c instanceof Coin);
console.log(c.constructor== Coin);
console.log(c);
console.log(Coin.Nickel);
console.log(Coin.values);
console.log(Coin.Nickel==5);
console.log(Coin.Nickel+Coin.Penny); //==6
</script>
</head>
<body>
</body>
</html>这里Coin.Nickel,Coin.Penny都是类属型,值是一个对象,为什么可以相加呢?
添加回答
举报
0/150
提交
取消