xinchou16 / demos Goto Github PK
View Code? Open in Web Editor NEWHome Page: https://xinchou16.github.io/demos/index.html
Home Page: https://xinchou16.github.io/demos/index.html
if (!Function.prototype.bind) {
Function.prototype.bind = function (obj) {
var self = this,
args = arguments;
// 返回一个函数
return function () {
self.apply(obj, [].slice.call(args, 1));
}
}
}
实现一个flatten函数,将一个嵌套多层的数组 array(数组) (嵌套可以是任何层数)转换为只有一层的数组,
数组中元素仅基本类型的元素或数组,不存在循环引用的情况。
flatten([1, [true], ['xin', [[4]]]]); //[1, true, "xin", 4]
var flatten = function(arr) {
let flattenArr = [];
function _flat(arr) {
arr.forEach( item => {
if(! Array.isArray(item)) {
flattenArr.push(item);
}else {
_flat(item);
}
})
}
_flat(arr); // 声明的一定要调用
return flattenArr;
}
flatten([1, [true], ['xin', [[4]]]]); //[1, true, "xin", 4]
var flatten = function(arr) {
return arr.reduce( (initArr,currentArr) => {
return initArr.concat( Array.isArray(currentArr) ? flatten(currentArr) : currentArr)
},[])
}
flatten([1, [true], ['xin', [[4]]]]); //[1, true, "xin", 4]
// 请用JavaScript 实现一个方法,该方法能够判断两个字符串是否匹配,
function isMatch(str1, str2) {
return str1.split('').sort().join('') === str1.split('').sort().join('');
}
isMatch('something', 'ginhtemos') // true
isMatch('aaa', 'aa') //false
isMatch('abb', 'baa') //false
isMatch('hello', 'olelh') //true
var object1 = {
valueOf: function () {
return 1;
},
toString: function () {
return "object1";
}
};
var object2 = {
valueOf: function () {
return 2;
},
toString: function () {
return "object2";
}
};
alert((object2 > object1 +-- object1) + true); //输出什么? 解释原因
function isEmptyObject(obj){
// todo...
}
isEmptyObject( {} ); //true
isEmptyObject( {a:1} ) ; //false
如果可以用 ES5,那么你会如何写这个函数?
// ES5
function isEmptyObject(obj) {
for( key in obj) {
if(obj.hasOwnProperty(key) && obj[key] != undefined) {
return false;
}
}
return true;
}
// ES6
function isEmptyObject(obj) {
return Object.keys(obj).length ? true : false;
}
isEmptyObject( {} ); //true
isEmptyObject( {a:1} ) ; //false
// JS运算符typeof相关问题
var str = new String("Hello");
var result = typeof(str instanceof String);
alert(result); //What is the output of the alert? 'Boolean'
result = typeof typeof(str instanceof String);
alert(result); //What is the output of the alert? 'String'
result = typeof typeof typeof(str instanceof String);
alert(result); //What is the output of the alert? 'String'
test
function Event() {
this.event = {};
}
Event.prototype = {
// 监听事件
on: function(attr,callback) {
if( this.event.hasOwnProperty(attr)) {
this.eventt[attr].push(callback)
}
this.event[attr] = [callback]
},
// 移除事件
off: function(attr) {
if(this.event.hasOwnProperty(attr)) {
delete this.event[attr]
}
},
// 触发事件
emit: function(attr, ...args) {
this.event[attr].forEach( cb => {
cb(args);
})
}
}
var event = new Event();
event.on('click', (arg) => {
console.log('clicked!'+arg)
});
event.emit('click','我是参数')
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.