ala888 Posted July 5, 2014 Share Posted July 5, 2014 How does JQUERY circumvent need for "new" statement. var x = new jquery(...); Is there a specific set of functions or whatever in the jquery prototype itself that automatically initiates the procedure?could someone link me ? Link to comment Share on other sites More sharing options...
thescientist Posted July 7, 2014 Share Posted July 7, 2014 why would it need new? jQuery is just a library / utility, it doesn't really need new in that context. Link to comment Share on other sites More sharing options...
ala888 Posted July 8, 2014 Author Share Posted July 8, 2014 Could you clear this up more please?Im having some trouble understanding.In javascript, if functions are just glorified object prototypes, then why is it not mandatory to declare all new functions as we do new objects? var x = new function(); and not function x (); Link to comment Share on other sites More sharing options...
Ingolme Posted July 8, 2014 Share Posted July 8, 2014 Actually, you can create a function using the new operator, but it's inefficient because it has to evaluate a string as code. var a = new Function("return 5"); But functions are more than just object prototypes. They are different because they can be called. They have a particular behavior that makes the keyword "function" a necessary and unique part of Javascript. jQuery is just an object, so you don't need the new keyword to load it. A simplified version of jQuery could be something like this: /* This is jQuery */var $ = function(param) { // Return an element or list of elements given "param"}/********************//* Now in your code you don't need the "new" keyword */var element = $("#container");/********************/ Link to comment Share on other sites More sharing options...
justsomeguy Posted July 8, 2014 Share Posted July 8, 2014 More specifically, the jQuery object is not a class, it is an instantiated object. You don't need to create a new object instance, it is already there. Link to comment Share on other sites More sharing options...
ala888 Posted July 8, 2014 Author Share Posted July 8, 2014 would jquery be this then ? var $ = function(selectorS) { var selector = selectorS; click = function(alert(x.selector));} $("HELLO WORLD").click(); I tried this out, sadly it does not work. why? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 8, 2014 Share Posted July 8, 2014 It's not that simple, you just defined a variable called click. Defining a variable doesn't make it a property of a function. I'm not sure how jQuery is specifically set up, I would imagine it's something like this though: var $ = function(opt) { this.click = function(callback) { alert('click'); if ((typeof callback) == 'function') { callback(); } return this; } return this;}$('test').click(function() {alert('callback')});Note the return statement at the end. It has to return itself because that's what allows you to chain things like $('').func1().func2().func3(). Each of those functions returns itself to keep the chain going. Link to comment Share on other sites More sharing options...
thescientist Posted July 9, 2014 Share Posted July 9, 2014 would jquery be this then ? var $ = function(selectorS) { var selector = selectorS; click = function(alert(x.selector));} $("HELLO WORLD").click(); I tried this out, sadly it does not work. why? you can always just view the source... https://github.com/jquery/jquery Link to comment Share on other sites More sharing options...
ala888 Posted July 9, 2014 Author Share Posted July 9, 2014 I dont use gitbut because I never understood the whole filing of it. Could you link me directly to the file itself? Link to comment Share on other sites More sharing options...
thescientist Posted July 9, 2014 Share Posted July 9, 2014 git is a version control system. a repo is location where code gets saved, using git. like for a project. github is a site for sharing repos / projects / source code. anyway, you can just view the "built" souce when you download the code from jquery too. http://jquery.com/ Link to comment Share on other sites More sharing options...
justsomeguy Posted July 9, 2014 Share Posted July 9, 2014 The production and developer versions are here:http://jquery.com/download/ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now