美文网首页
jquery揭秘

jquery揭秘

作者: 深沉的简单 | 来源:发表于2016-12-26 11:38 被阅读13次

jQuery.extend()

**描述: **将两个或更多对象的内容合并到第一个对象。

方法 描述
$.extend({}, {a:1,b:2}, {a:10,b:20}) 1.{} 传递空的对象
2. {a:1,b:2}传递对象默认值
3.{a:10,b:20}能替换第二个默认值参数

$.extend(1,2,3)方法,在没有传递第三个参数的情况下会返回第二个默认对象,传递了第三个值会替换第二个对象。这样能解决默认参数的问题。

传递默认值

var object = $.extend({}, {a:1,b:2});
//Object {a: 1, b: 2}

传递替换默认值

var object = $.extend({}, {a:1,b:2}, {a:10,b:20});
//Object {a: 10, b: 20}

jQuery.proxy() 实用工具

**描述: ** 通过jQuery.proxy() 实用工具改变this指向

方法 描述
$.proxy(opi.c,opi) 1.调用方法
2.this指向

在没使用前如果调用方法是事件对象,那么函数原型链上的this是无法指向这个构造函数本身的,this会指向事件按钮本身

<button id="bnt">按钮</button>
<script type="text/javascript">
    function oppo(){
        this.x=1
    }
    oppo.prototype.c=function(){
        console.log(this)
        return this.x*10
    }
    var opi=new oppo()

    $(function(){
        $("#bnt").on('click',opi.c)
    })
</script>
Paste_Image.png

使用$.proxy(opi.c,opi)方法第一个参数传入方法第二个传入构造器本身也就是将this指向了构造器。

<button id="bnt">按钮</button>
<script type="text/javascript">
    function oppo(){
        this.x=1
    }
    oppo.prototype.c=function(){
        console.log(this)
        return this.x*10
    }
    var opi=new oppo()

    $(function(){
        $("#bnt").on('click',$.proxy(opi.c,opi))
    })
</script>
Paste_Image.png

if(!this.$el.is(':animated'))

**描述: ** 判断元素是否在执行中

如果正在运动,返回true,只有不在运动的时候返回false

相关文章

网友评论

      本文标题:jquery揭秘

      本文链接:https://www.haomeiwen.com/subject/luljvttx.html