Jump to content

How does JQUERY circumvent need for "new"


ala888

Recommended Posts

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

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

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...