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

javascript - Do we 'declare' jQuery or we simply use it? - Stack Overflow

programmeradmin6浏览0评论

I was asked in an interview that how do you 'declare' jQuery? He did not mean a jQuery variable or a $(func()). In case you find this question weird, please do not penalize me for this as I'm enquiring just cause I was asked. :)

I was asked in an interview that how do you 'declare' jQuery? He did not mean a jQuery variable or a $(func()). In case you find this question weird, please do not penalize me for this as I'm enquiring just cause I was asked. :)

Share Improve this question edited Dec 21, 2011 at 9:38 Michał Powaga 23.2k8 gold badges53 silver badges62 bronze badges asked Dec 21, 2011 at 7:09 RohanRohan 1,8057 gold badges18 silver badges39 bronze badges 6
  • Perhaps he meant how one would include jQuery? As in, how to use a <script> tag to reference it? – Interrobang Commented Dec 21, 2011 at 7:10
  • @Interrobang: Thats what I told him. You do not have to declare. You simple add the latest jquery.js file and call doc ready function. He did not seem to be convinced. Such questions make one lose confidence when one is new to something :) – Rohan Commented Dec 21, 2011 at 7:13
  • I agree. That almost sounds like an HR guy that was given a list of screening questions to ask that he didn't really understand. You're in the right, carry on! – Interrobang Commented Dec 21, 2011 at 7:14
  • I would say both. 'var ctr = $("#txLogin")' but also directly, depends on use. – Independent Commented Dec 21, 2011 at 7:25
  • @Rohan - It sounds like you gave the correct answer (noting though that you don't have to call doc ready). If he didn't seem convinced either he was looking for an answer so obscure that nobody here knows it either, or Interrobang is correct that he didn't understand what he was asking. Or both. I've had interviews where a non-technical person just read technical questions off the page. – nnnnnn Commented Dec 21, 2011 at 7:44
 |  Show 1 more comment

4 Answers 4

Reset to default 15

You don't "declare" jQuery, you just include the file within a script tag:

<script src="/locationof/jQuery.js"></script>

If you look in the jQuery source it appends itself to window.$ and window.jQuery when it runs as the source code is in a self-executing anonymous function.

(function( window, undefined ) {
   // rest of source here

   // Expose jQuery to the global object
   window.jQuery = window.$ = jQuery;
})(window);

We don't "declare" jQuery, we just add "reference" to the file where is this library in. For example:

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>

That's it.

Perhaps he meant how to include it:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

I guess he was assuming that you say using a $ sign

发布评论

评论列表(0)

  1. 暂无评论