英文不好,大家见谅
My english is not good!= =|||
//1.JS中Array自定义函数的问题
//About Array custom function In JS.
Array.prototype.contains = function ( needle ) {
for (var i = 0; i < this.length; ++i) {
if (this[i] == needle) return true;
}
return false;
};
// demo
var arr = [1, 2];
this.say("=="+arr.contains);
this.say(arr.contains(2));
//arr.contains没有定义
//arr.contains undefined
//2. JS中print object的问题
//print object in JS
var print = function(o, deep, cur_deep){
if(!arguments[1]) deep = 9;
if(!arguments[2]) cur_deep = 0;
var comma = ',';
var newline = '\r\n';
var str='';
var is_array = function(o) { return Object.prototype.toString.call(o) === '[object Array]'; };
var len = function(o){
if(o.hasOwnProperty('length')){
return o.length
}else if(o.hasOwnProperty('__count__')){
return o.__count__;
}else{
var count = 0;
for (var i in o)
count ++;
return count;
}
}
var t = typeof o;
if(t == 'string'){
str += o;
}else if(t == 'object'){
if(cur_deep < deep)
{
var bArr = is_array(o);
++cur_deep;
str += bArr ? '[' : '{';
//if(!bArr){ str += newline; }
var count = len(o);
var i = 0;
for(var p in o){
if(!bArr){ str += p + ': '; }
//str += print(bArr ? p : o[p], deep, cur_deep); //This line of mistake, I fixed
str += print(o[p], deep, cur_deep);
if(i++ != count - 1){
str += comma;
//if(!bArr){ str += newline; }
}
}
//if(!bArr){ str += newline; }
str += bArr ? ']' : '}';
--cur_deep;
}else{
str+= '<object>';
}
}else /*if(t == 'function' || t == 'number')*/{
str += o.toString();
}
return str;
};
this.print = function(o, deep, cur_deep){ this.say(print(o, deep, cur_deep)); };
//demo
this.print([this,this]);
// 1>this.say内容过长,只能显示...
// the content of the "this.say" function is too long, can only display"..."
// 2> this.say中怎么换行,用\r\n还是<br/>,没效果
// how to use newline in say; "\r\n" or <br/>,no effect
//3. JS中foreach问题
// About foreach in JS
var enemies = this.findEnemies();
// error code
for(var e in enemies) // foreach
this.print(e);
// correct code
for(var i = 0; i < enemies.length; ++i)
this.print(enemies[i]);