(五)引用类型

1. 使用对象字面量创建对象、使用数组字面量创建数组的时候

// 使用 对象字面量创建对象
var person = {
  name: 'zhoufangbing',
  age: 22
}

不会调用 Object 的构造函数 

// 使用数组字面量创建数组
var arr = [1, 2, 3]

不会调用 Array 的构造函数

new Object()、new Array()的会调用构造函数~new 的时候会

 

2. 数组类型检测: 2种方法

(1)instanceof

(2)Array.isArray()

var a = [1, 2, 3]
var b = {name: 'zhoufangbing'}
console.log(a instanceof Array) // true
console.log(b instanceof Array) // false
console.log(Array.isArray(a)) // true
console.log(Array.isArray(b)) // false

判断对象不能用 instanceof ~因为数组也是对象 不准确

3.计算所间隔的时间

Date.now()

function running(val) {
      console.log(typeof val)
      if (typeof val === 'string') {
        console.log('hello,' + val)
      } else if (typeof val === 'number') {
        console.log('age,' + val)
      }
    }
    var start = Date.now()
    running('zhoufangbing')
    var stop = Date.now()
    const result = stop - start
    console.log('用时', result)

4. 函数带括号与不带括号的区别

将函数做为参数传递给另外一个函数时,该函数不用写括号,原因:如果写了括号,相当于把函数的执行返回结果,做为参数传入,而不是传入一个函数对象本身了。没写括号传函数自身,写了括号传结果

函数带括号立即执行,有return 即返回函数结果,没return执行函数功能

5. 一个函数中返回另一个函数 ???

function createComparisonFunction(propertyName) {
      return function (object1, object2) {
        var val1 = object1[propertyName]
        var val2 = object2[propertyName]
        if (val1 < val2) {
          // 从小到大
          return -1
        } else if (val1 > val2) {
          // 从大到小
          return 1
        } else {
          return 0
        }
      }
    }

    var data = [{
      age: 28,
      name: 'Nick'
    }, {
      age: 29,
      name: 'Jame'
    }]

    const result = data.sort(createComparisonFunction('age'))
    console.log(result)

按照 age 值从小到大进行排序~

6. 为什么 arguments.callee() 代表函数本身?

arguments主要作用是保存对象的参数,它有一个.callee的属性,这个属性是一个指针,指向拥有这个arguments 对象的函数~

7. this指向的是函数的执行环境对象

8. 函数.length 是返回函数参数的个数

function sum (a, b) {
  return a + b
}
console.log(sum.length) // 2 需要两个参数

9. call()、apply() 作用:扩大函数的作用域 想让函数的作用域是谁,就把谁作为第一个参数传给 call、apply

window.color = 'red'
    var o = {
      color: 'blue'
    }
    function sayColor() {
      return this.color
    }
    // 此时 this 指向 window window.sayColor sayColor 中的this 指向window 输出 red
    console.log(sayColor.call(this)) // red
    // 此时 this 指向 o o.sayColor sayColor 中的this 指向o 输出 red
    console.log(sayColor.call(o)) // blue

10. 

// 以下有什么不同
var a = 10
var b = new Number(10)

虽然 都是10, 但是实例化和直接赋值的区别 typeof 和 instanceof 有不同

var a = 10
var b = new Number(10)
console.log(typeof(a)) // number
console.log(typeof(a instanceof Number)) // true
console.log(typeof(b)) // object
console.log(typeof(b instanceof Number)) // false

11. 返回一个字符串中所有的匹配字符位置

var str = 'hello world e e e hello you'
    var pos = str.indexOf('e')
    var indexArr = []
    while (pos > -1) {  
      indexArr.push(pos)
      pos = str.indexOf('e', pos + 1)
    }
    console.log(indexArr) // [1, 12, 14, 16, 19]

indexof: 第一个参数是要查找的字符,第二个字符是要开始查找的位置~

12. 使用apply 求数组最大值

var arr = [1,2,4,57,8,99]
var max = Math.max.apply(Math, arr)
var min = Math.min.apply(Math, arr)
console.log(max, min) // 99 1

apply 将this 指向 Math 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值