Just jQuery The Core UI - The JQuery Object |
Written by Ian Elliot | |||
Saturday, 24 September 2022 | |||
Page 1 of 2 The JQuery object is a wonderful example of just how sophisticated JavaScript can be. It is worth knowing how to use it and how it works. This is an extract from my book Just jQuery The Core UI. Available as a Book:
buy from Amazon
Also Available:buy from Amazon
The way that jQuery is implemented is very clever and very JavaScript. It is worth understanding the jQuery object at a deeper level, partly because then you will know how best to use jQuery and what can go wrong. Even more important is that jQuery provides an example of how to use JavaScript properly. It is a technique worth using in your own programs. Before diving into jQuery it will help to go over some standard, but often overlooked, JavaScript features. The Function ObjectOne of the key features of JavaScript that makes it very different from other programming languages is that “functions are first class objects”. This is often quoted but what does it mean? In JavaScript an object has properties, some of which can be functions, and roughly speaking everything in JavaScript is an object including functions. Normally you define a function using something like: myFunction(){ statements } but this is a form of expression introduced to make JavaScript look like other languages. You can also define a function using: var myFunction= new Function(statements); or just: var myFunction=function(){ These two forms emphasize that a function is just an instance of a Function object. What this means in practice is that a function can have properties. For example: var sum=function(a,b){ defines a simple function to add two numbers together. You can, however, add to it any number of properties you like. For example: sum.bias=100; introduces two new properties, bias and author, to the sum Function object. These can be accessed in the usual way: var result=sum.bias+100; Notice that when we are accessing a property you just use the variable: sum.author; but when you want to call the function you use parentheses: sum() It is helpful to think of () as the function evaluation operator. That is, when you write: object(); it means evaluate the function represented by object. Of course, if object isn’t a Function this doesn’t work. You can include arguments to be passed to the function within the evaluation operator. As well having simple properties, a Function object can also have properties which are Function objects – and this is the most confusing part of all. For example: sum.diff=function(a,b){ Now the sum Function has a property, diff, that is a Function object in its own right. You can call this function in the usual way: sum.diff(4,2); So now we can call sum or sum.diff. In other languages sum.diff would be called a method of the sum object. What is different about JavaScript is that sum is also a Function in its own right. To summarize:
|
|||
Last Updated ( Saturday, 24 September 2022 ) |