<!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都是類屬型,值是一個(gè)對(duì)象,為什么可以相加呢?
求解釋下面js中的枚舉類型的代碼是什么意思?
徐錦杰
2017-03-03 23:39:07