前端每日一题

6.分割字符串为所有可能的数组

输入字符串‘abc’

输出

[

['a','b','c'],

['ab','c'],

['a','bc']

['abc']

]

输入 ‘abcc’

输出

[

['a','b','c','c'],

['a','bc','c'],

['a','bcc'],

['ab','c','c'],

['ab','cc'],

['abc','c'],

['abcc']

]

5.改写add函数

日期:2019.5.10

add(1) //1

add(1)(2) //2

add(1)(2)(3) //3

答案:

function add(a){
        var sum = a;
        function temp(b){
            sum= b || a
            return temp;
        }
        temp.toString = function(){
            return sum;
        }
        return temp;
    }
    var a = add(1)(2)(3)
    console.log(a.toString()) //6
View Code

4.实现add函数  

日期:2019.5.10

add(1) // 1

 add(1)(2) //3  

add(1)(2)(3)//6

答案

	function add(a){
		var sum = a;
		function temp(b){
			sum+=b
			return temp;
		}
		temp.toString = function(){
			return sum;
		}
		return temp;
	}
	var a = add(1)(2)(3)
	console.log(a.toString()) //6

  

3.setTimeout中this的指向

日期:2019.5.10

    var name = "windowsName";

    var a = {
        name : "Cherry",
        func1: function () {
            console.log(this.name)     
        },

        func2: function () {
            setTimeout(  function () {
                this.func1()
            },100);
        }
    };
    var b = a.func2

  问题:使用b函数输出 Cherry,使用b函数输出windowsName

    var name = "windowsName";
    var that = this
    var a = {
        name : "Cherry",
        func1: function () {
            console.log(this.name)  //cherry
            console.log(that.name)	//windowsName
        },

        func2: function () {
            setTimeout(  function () {
                this.func1()
            }.bind(a),100);
        }
    };
    var b = a.func2
    b()

  

2019/4/26

1.数组对象查询

现在有数组arr如下,要求输入关键字查询所有符合的对象并以数组对象的形式返回

      var arr = [{
        id:'1',
        name:'小明',
        tel:'3996',
        hobby:'读书'
      },{
        id:'2',
        name:'小红',
        tel:'2289',
        hobby:'打代码'
      },{
        id:'3',
        name:'小黑',
        tel:'666',
        hobby:'打代码'
      }]
View Code

输入关键字 3 则返回

[{
        id:'1',
        name:'小明',
        tel:'3996',
        hobby:'读书'
      },{
        id:'3',
        name:'小黑',
        tel:'666',
        hobby:'打代码'
      }]
View Code

答案如下

      var arr = [{
        id:'1',
        name:'小明',
        tel:'3996',
        hobby:'读书'
      },{
        id:'2',
        name:'小红',
        tel:'2289',
        hobby:'打代码'
      },{
        id:'3',
        name:'小黑',
        tel:'666',
        hobby:'打代码'
      }]

      var newArr = arr.filter(item=>{
        var tempItem = JSON.stringify(item)
        if (tempItem.indexOf('读书') !== -1) return item
      })
      console.log(newArr) //[{id:'1',name:'小明',tel:'3996',hobby:'读书'}]
      }
View Code

 2019/5/8

2.去除字符串首尾空格

    var s = ' 1 23 456 '
    console.log('s:',s.replace(/(^s*)|(s*$)/g, ""))

  

原文地址:https://www.cnblogs.com/zwyboom/p/10773800.html