GithubHelp home page GithubHelp logo

demos's Introduction

Hi there 👋

XinChou's Github Stats

demos's People

Contributors

xinchou16 avatar

Watchers

 avatar

demos's Issues

JS-Polyfill

1. bind

if (!Function.prototype.bind) {
  Function.prototype.bind = function (obj) {
    var self = this,
        args = arguments;
    
    // 返回一个函数
    return function () {
      self.apply(obj, [].slice.call(args, 1));
    }
  }
}

JS-数组方法的实现

实现一个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]

JS-字符串

判断字符串是否匹配

// 请用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

JS-对象

1.如下代码输出什么? 解释原因

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); //输出什么? 解释原因
  1. 对object进行运算时,会调用valueof方法,由于都存在这个方法,所以调用这个函数
  2. --优先级别最高,首先执行 ( 2 > 1 + --1) + true
  3. true在进行数值运算转为数值1
  4. ( 2 > 1 + 0) + 1 最终结果为2

2.写一个函数 isEmptyObject,判断一个对象是不是空对象

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 相关问题

// 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'

JS-util

sub,sup实现

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','我是参数')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.