Excuse me first. because i don’t know this is question is valid or not. i if any one clear my doubt then i am happy.
Basically : what is the different between calling a method like:
-
object.methodname();
-
$(‘#element’).methodname();
calling both way is working, but what is the different between, in which criteria make first and second type of methods. is it available in the core javascript as well?
In case if i have a function is it possible to make 2 type of method call always?
Can any one give some good reference to understand correctly?
Thanks in advance.
The first syntax:
Says to call a function,
methodName(), that is defined as a property ofobject.The second syntax:
Says to call a function called
$()which (in order for this to work) must return an object and then callmethodname()on that returned object.You said that “calling both way is working,” – so presumably you’ve got some code something like this:
This concept of storing the result of the
$()function in a variable is commonly called “caching” the jQuery object, and is more efficient if you plan to call a lot of methods on that object because every time you call the jQuery$()function it creates another jQuery object.“Is it available in the core javascript as well?” Yes, if you implement functions that return objects. That is, JS supports this (it would have to, since jQuery is just a JS library) but it doesn’t happen automatically, you have to write appropriate function code. For example:
In my opinion explaining this concept in more depth is beyond the scope of a Stack Overflow answer – you need to read some JavaScript tutorials. MDN’s Working With Objects article is a good place to start once you have learned the JS fundamentals (it could be argued that working with objects is a JS fundamental, but obviously I mean even more fundamental stuff than that).