在javascript中实现OO,通过prototype为类型添加方法,好像继承
2008-03-23 17:15 Virus-BeautyCode 阅读(513) 评论(0) 编辑 收藏 举报<script type="text/javascript">
var sample = "SomeThing";
function StrangeCaseIt() {
var returnVal = "";
for(var i = 0; i < this.length; i++) {
if(this.substr(i, 1).toUpperCase() == this.substr(i, 1)) {
returnVal += this.substr(i, 1).toLowerCase();
}
else {
returnVal += this.substr(i, 1).toUpperCase();
}
}
return returnVal;
}
String.prototype.StrangeCaseIt = StrangeCaseIt;
var result = sample.StrangeCaseIt();
document.write(sample + "<br />");
document.write(result + "<br />");
</script>
这段脚本为string类型添加了方法,StrangeCaseIt
var sample = "SomeThing";
function StrangeCaseIt() {
var returnVal = "";
for(var i = 0; i < this.length; i++) {
if(this.substr(i, 1).toUpperCase() == this.substr(i, 1)) {
returnVal += this.substr(i, 1).toLowerCase();
}
else {
returnVal += this.substr(i, 1).toUpperCase();
}
}
return returnVal;
}
String.prototype.StrangeCaseIt = StrangeCaseIt;
var result = sample.StrangeCaseIt();
document.write(sample + "<br />");
document.write(result + "<br />");
</script>