Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Sunday, March 13, 2011

JavaScript : Comaprison of Objects


JavaScript has two different ways of comparing the values of objects for equality.
The equals operator
The equals operator consists of two equal signs: ==
JavaScript features weak typing, that means, that the equals operator coerces types in order to compare them.

""           ==   "0"           // false
0            ==   ""            // true
0            ==   "0"           // true
false        ==   "false"       // false
false        ==   "0"           // true
false        ==   undefined     // false
false        ==   null          // false
null         ==   undefined     // true
" \t\r\n"    ==   0             // true
The above table shows the results of the type coercion and it is the main reason why the use of == is widely regarded as bad practice,
it introduces hard to track down bugs due to its complicated conversion rules.
Additionally there is also a performance impact when type coercion is in play;
for example, a string has to be converted to a number before it can be compared to another number.

The strict equals operator
The strict equals operator consists of three equal signs: ===
Other than the normal equals operator, the strict equals operator does not perform type coercion between its operands.


""           ===   "0"           // false
0            ===   ""            // false
0            ===   "0"           // false
false        ===   "false"       // false
false        ===   "0"           // false
false        ===   undefined     // false
false        ===   null          // false
null         ===   undefined     // false
" \t\r\n"    ===   0             // false
The above results are a lot clearer and allow for early breakage of code.
This hardens code to a certain degree and also gives performance improvements in case the operands are of different types.
Comparing objects
While both == and === are stated as equality operators, they behave different when at least one of their operands happens to be an Object.

{} === {};                            // false
new String('foo') === 'foo';  // false
new Number(10) === 10;   // false
var foo = {};foo === foo;    // true
Here both operators compare for identity and not equality; that is, they will compare for the same instance of the object, much like is in Python and a pointer comparison in C do.
Conclusion
It is highly recommended to only use the strict equals operator. In cases where types need to be coerced, it should be done explicitly and not left to the language's complicated coercion rules.

JavaScript Parser - Sometimes Intelligence causes problems...



JavaScript parser automatically inserts semicolon whenever   it encounters a   parse error due to missing semicolons.

var foo = function() {
} // parse error, semicolon expected
test()
Insertion happens, and the parser tries again.
var foo = function() {
}; // no error, parser continues
test()
The automatic insertion of semicolon is considered to be one of biggest design flaws in the language, as it can change the behavior of code.

How it works

The code below has no semicolons in it, so it is up to the parser to decide where to insert them.
(function(window, undefined) {
    function test(options) {
        writeToLog('testing semicolons...!')

        (options.list || []).forEach(function(i) {

        })

        options.value.test(
            'long string to pass here',
            'and another long string to pass'
        )

        return
        {
            foo: function() {}
        }
    }
    window.test = test
})(window)
(function(window) {
    window.someLibrary = {}
})(window)
Below is the result of the parser's "guessing" game.

Monday, January 17, 2011

JavaScript Differences between IE and Mozilla - Tutorial 1 on Web Browser Differences

Web Programming on various browsers is a challenging task ( It is relatively easy now with utilities like JQuery) , but for older legacy applicatons - this will give a quick view of what works in IE  and what is the alternative in Mozilla

                             IE                                                                               Mozilla
Get Element by ID Name
document.all
document.getElementById

Get All Elements on a Page
use the document.all array
document.getElementById('*')

Get all child elements from a node
element.children works in IE to get all HTML 
elements. element.childNodes works in all
 browsers but returns all nodes, 
not just HTML elements.


Make one node the child of another
applyElement (appendChild works too)
appendChild

Create an XMLHttp Object
new ActiveXObject(ObjID);
ObjID can be one of ['Msxml2.XMLHTTP.6.0', 
'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', '
Microsoft.XMLHTTP'], 
depending on what the client has installed
new XMLHttpRequest();

Set or read the text value of an element
innerText
textContent

Read/Set the float property of an element
style.cssFloat
style.styleFloat

Get the event object in a function
window.event
must be passed to the function

Get the target of an event
event.srcElement
event.target

Get the mouse position on screen
event.clientX+object.scrollLeft-object.clientLeft
and
event.clientY+object.scrollTop-object.clientTop
(phew!)
event.pageX and event.pageY

Get window size
document.body.clientHeight or document.documentElement.clientHeight, 
depending on the doctype
window.innerHeight and window.innerWidth

One way to set position/size of an element
pixelLeft, pixelTop, pixelHeight and pixelWidth
left, top, height and width

subversion video