js代码求解释下面这个例子
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><scripttype="text/javas...
<!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;}
};//proto是enumeration的原型
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);
</script>
</head>
<body>
</body>
</html>
看不太懂这个例子,能解释一下吗?
尤其看不懂这句enumeration[name]=e;的作用 展开
<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;}
};//proto是enumeration的原型
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);
</script>
</head>
<body>
</body>
</html>
看不太懂这个例子,能解释一下吗?
尤其看不懂这句enumeration[name]=e;的作用 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询