Jquery

Posted by Learning libs on January 12, 2020

JQuery的extend扩展方法

  1. $.extend(src)

该方法就是将src合并到jquery的全局对象中去,如:

  $.extend({
    hello:function(){alert('hello');}
  });
  // 就是将hello方法合并到jquery的全局对象中

  $.hello() // 弹出 hello

  1. $.fn.extend(src)

该方法将src合并到jquery的实例对象中去,如:

  $.fn.extend({
    hello:function(){alert('hello');}
  });
  // 就是将hello方法合并到jquery的实例对象中。
  var obj = new jQuery()
  obj.hello() // 弹出 hello

  // 在jquery全局对象中扩展一个net命名空间
  $.extend({
    net:{}
  })

  // hi 方法扩展到之前扩展的Jquery的net命名空间中去
  $.extend(
    $.net, {
      hi: function(){alert('hi')}}
  )
  $.net.hi()

  1. JQuery的extend方法还有一个重载原型

extend(boolean,dest,src1,src2,src3...)

第一个参数boolean代表是否进行深度拷贝

  var src1 = {name: 'John', family:'Wu', location: {city: 'Shanghai', county: 'China', province: 'shanghai'}}
  var src2 = {name: 'Judy', last: 'Yuan', location: {city: 'Boston', county: 'USA', time:'NY'}}
  // 浅拷贝
  var result1 = $.extend(false, {}, 
    src1, src2        
  )
  console.log(result1)

  // 输出
  {
    family: "Wu"
    last: "Yuan"
    location: {city: "Boston", county: "USA", time: "NY"}
    name: "Judy"
  }

  // 深拷贝
  var result2 = $.extend(true, {}, 
    src1, src2        
  )
  console.log(result2)

  // 输出
  {
    family: "Wu"
    last: "Yuan"
    location: {city: "Boston", county: "USA", province: "shanghai", time: "NY"}
    name: "Judy"
  }


typeof()与$.type()的区别

  typeof ['1','2']
  输出: "object"

  $.type(['1','2'])
  输出: "array"

  Object.prototype.toString.call(['1','2','3'])
  输出: "[object Array]"

$.isFunction(), jQuery.isFunction() 函数用于判断指定参数是否是一个函数,返回值为Boolean类型