最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery: $(this) vs. this.$() - Stack Overflow

programmeradmin3浏览0评论

In Ember.js's docs, they have a jQuery code snippet with the following syntax:

this.$().button();

Is this snippet only turning this into a jQuery object so that the jQuery UI .button() function can be called on it?

Would this snippet be identical?

$(this).button();

In Ember.js's docs, they have a jQuery code snippet with the following syntax:

this.$().button();

Is this snippet only turning this into a jQuery object so that the jQuery UI .button() function can be called on it?

Would this snippet be identical?

$(this).button();
Share Improve this question edited Jun 18, 2012 at 21:24 Noah Freitas asked Jun 18, 2012 at 21:13 Noah FreitasNoah Freitas 17.4k11 gold badges51 silver badges67 bronze badges 4
  • The first snippet suggests that the jQuery object ($) is stored as a property on this, possibly to avoid polluting global scope, but I'm not sure. – chrisfrancis27 Commented Jun 18, 2012 at 21:16
  • But it's executed. And it returns, so it's chained. I think that's legit, but I would never have thought of trying that ... – jcolebrand Commented Jun 18, 2012 at 21:17
  • does this.button() work? If so, 'this' is a jquery object. – MMeah Commented Jun 18, 2012 at 21:25
  • Well, I just tested the "random this with jquery() as a function on it" and of course, as expected, that doesn't work – jcolebrand Commented Jun 18, 2012 at 21:26
Add a comment  | 

1 Answer 1

Reset to default 26

The source code explains this as follows:

/**
    Returns a jQuery object for this view's element. If you pass in a selector
    string, this method will return a jQuery object, using the current element
    as its buffer.

    For example, calling `view.$('li')` will return a jQuery object containing
    all of the `li` elements inside the DOM element of this view.

    @param {String} [selector] a jQuery-compatible selector string
    @returns {Ember.CoreQuery} the CoreQuery object for the DOM node
  */
  $: function(sel) {
    return this.invokeForState('$', sel);
  },

So to answer your question: no it's not the same as $(this), which would wrap the ember view instance in a jQuery object...

发布评论

评论列表(0)

  1. 暂无评论