Skip to content

Instantly share code, notes, and snippets.

@findyourmagic
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save findyourmagic/6a849b389777590e22cd to your computer and use it in GitHub Desktop.

Select an option

Save findyourmagic/6a849b389777590e22cd to your computer and use it in GitHub Desktop.
javascript fragment
var arr = [1,2,3,[1,2,3]];
var objToString = Object.prototype.toString;
function getType( obj ){
var str = objToString.call(obj);
str = str.split(' ')[1];
str = str.slice(0,str.length - 1);
return str.toLowerCase();
}
function copyObj( obj){
if ( getType(obj) == 'array' ){
var newObj = [];
}else if ( getType(obj) == 'object') {
var newObj = {};
}else{
return obj;
};
for ( var i in obj){
(function(){
var item = obj[i];
if ( getType(item) == 'array' || getType(item) == 'object' ){
newObj[i] = copyObj(item);
}else{
newObj[i] = item;
}
})();
}
return newObj;
}
var arr1 = copyObj(arr);
var getSearch = function(){
var search = window.location.search;
var reg = /([^\=\?\&]+)\=([^\=\?\&]+)/g;
var obj = {};
search.replace(reg , function( item, key, value){
obj[key] = decodeURIComponent(value);
return item;
});
return obj;
}
function keyUp(e) {
var currKey=0,e=e||event;
var currKey=e.keyCode||e.which||e.charCode;
var keyName = String.fromCharCode(currKey);
alert("按键码: " + currKey + " 字符: " + keyName);
}
document.onkeyup = keyUp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment