Array.prototype.unique = function() {
var temp = {},
res = [];
for (var i = 0, len = this.length; i < len; i++) {
if (!temp[this[i]]) {
temp[this[i]] = true;
res.push(this[i]);
}
}
return res;
}
console.log([1, 1, 1, 2, 4, 4, 4, 2].unique());
function clone(obj) {
var arr = [],
res = {};
if (obj instanceof Array) {
obj.forEach(function(item, i) {
arr[i] = clone(item);
})
return arr;
} else if (obj instanceof Object) {
for (var j in obj) {
res[j] = clone(obj[j]);
}
return res;
} else {
return obj;
}
}
var map = {
"name": "jack"
};
console.info(clone(map));
var notice = {
handlers: {},
on: function(type, params) {
var funs = this.handlers[type];
if (!funs) {
console.log("未注册此事件")
}
funs.forEach(function(item, i) {
item.func.apply(item.tg, params || []);
})
},
send: function(type, fun, tg) {
if (!this.handlers[type]) {
this.handlers[type] = [];
}
this.handlers[type].push({
tg: tg || this,
func: fun
})
}
}
notice.send("run", function() {
console.log("I’m running");
})
notice.on("run");
function isArray(arr){
return Array.isArray(arr) || Object.prototype.toString.call(arr) === '[object Array]';
}
function isString(str){
return Object.prototype.toString.call(str) === '[object String]';
}
function random(min,max){
return Math.floor(Math.random()*(max-min) +1);
}
random(1,10);
function toArray(){
return Array.prototype.slice.call(arguments);
}
function sum(){
var arr = Array.prototype.slice.call(arguments);
var res = 0;
arr.forEach(function(item,i){
res+=item;
})
return res;
}
sum(1,2,3,4);
var arr=[13,24,3,5,56,78,9];
arr.sort(function(x,y){
return x-y;
})
console.log(arr)
var str1 = "上海在海上",
str2 = "北京在北京";
function ishuiwen(str){
var tempStr = str.split("").reverse().join("");
if(tempStr === str){
return true;
}else{
return false;
}
}
ishuiwen(str1);
ishuiwen(str2);
var count = function(){
var n = 0;
return function(){
n++;
return n;
}
}
count()()
var count = (function(){
var n = 0;
return function(){
n++;
return n;
}
})()
count()