1)explain javascript inheritance ?
JavaScript does not have “methods” in the form that class-based languages define them. In JavaScript, any function can be added to an object in the form of a property. An inherited function acts just as any other property When an inherited function is executed, the value of this points to the inheriting object, not to the prototype object where the function is an own property.
var o = { a: 2, m: function(b){ return this.a + 1; } };
console.log(o.m()); // 3 // When calling o.m in this case, ‘this’ refers to o
var p = Object.create(o); // p is an object that inherits from o
p.a = 12; // creates an own property ‘a’ on p console.log(p.m()); // 13 // when p.m is called, ‘this’ refers to p. // So when p inherits the function m of o, ‘this.a’ means p.a, the own property ‘a’ of p
2) Diff b/w $document.ready and window.onload and body onload event ? Windows load event fires when all the content on your page fully loaded including the DOM (document object model) content, asynchronous JavaScript, frames and images, you can also use body onload= both are same just window.onload = func(){} and are different ways of using the same event.
jQuery $document.ready function event executes a bit earlier than window.onload and called once the DOM(Document object model) is loaded on your page. It will not wait for the images, frames to get fully load.
3)explain Javascript static variables and methods ? Static variables and methods
Static variables Static methods
A function is an object. That provides us with a very good way to create static variables or the variables which persist along multiple calls.
For example, we want a variable which counts function calls. Static variables
There are languages which allow to put a static keyword before a variable, and then such variable is not cleared in next calls.
Example of a static variable in PHP language: 1 function f() { // PHP code! 2 static $count = 0; 3 4 echo ++$count; 5 } 6 7 f(); f(); f(); // 1 2 3
In JavaScript, there is no term or keyword static, but we can put such data directly into function object (like in any other object).
1 function f() { 2 f.count = ++f.count || 1 // f.count is undefined at first 3 4 alert(“Call No ” + f.count) 5 } 6 7 f(); // Call No 1 8 f(); // Call No 2
Of course, a global variable can keep the counter, but static variables lead to a better architecture.
We could make the code more universal by replacing f with arguments.callee.
1 function f() { 2 arguments.callee.count = ++arguments.callee.count || 1 3 4 alert(“Called ” + arguments.callee.count + ” times”) 5 }
Now you can safely rename the function if needed Smile Static methods
Static methods, just like variables, are attached to functions. They are used mostly for objects:
01 function Animal(name) { 02 arguments.callee.count = ++arguments.callee.count || 1 03 04 this.name = name 05 } 06 07 Animal.showCount = function() { 08 alert( Animal.count ) 09 } 10 11 var mouse = new Animal(“Mouse”) 12 var elephant = new Animal(“elephant”) 13 14 Animal.showCount() // 2
for more info : http://javascript.info/tutorial/static-variables-methods-decorators
4) If both parent and child element have onclick event , when you click on child , parent onclick event also loads. how to disable it ?
using event.stopPropagation() we can stop the event bubbling up in DOM tree. $( .childelementclass ).click(function( event ) { event.stopPropagation(); // Do something });
For more info : http://api.jquery.com/event. stopPropagation/
5) What does the $ sign mean in Jquery ?
The $ is just a function. It is actually an alias for the function called jQuery, so your code can be written like this with the exact same results:
jQuery(‘#Text’).click(function () { jQuery(‘#Text’).css(‘color’, ‘red’); });
In jQuery the variable is assigned a copy of the jQuery function. This function is heavily overloaded and means half a dozen different things depending on what arguments it is passed. In this particular example you are passing it a string that contains a selector, so the function means “Create a jQuery object containing the element with the id Text”
6) explain javascript namespace ? “what is a namespace?” A namespace is a container and allows you to bundle up all your functionality using a unique name. In JavaScript, a namespace is really just an object that you’ve attached all further methods, properties and objects. Creating a namespace for your code helps prevent the overwriting of existing function names. It’s also a step in the object-oriented direction, which promotes modularity and code re-use.
7) whather Javascript supports block level scope or not ? Blocks do not have scope in Javascript .
There is no difference between the following:
1 var i = 1 2 { 3 i = 5 4 }
…And the following 1 i = 1 2 { 3 var i = 5 4 }
All var declarations are processed before the execution in in both cases.
Unlike languages like Java, C etc, variables in JavaScript survive after a loop.
That’s again, because their scope is a function.
1 for(var i=0; i<5; i++) { } 2 3 alert(i) // 5, variable survives and keeps value
Declaring a variable in the loop is convenient, but doesn’t make the loop it’s scope. for mor einfo : http://javascript.info/tutorial/initialization#blocks-do-not-have-scope
8) why Javascript is loosely typed language ? Loose typing means that variables are declared without a type. This is in contrast to strongly typed languages that require typed declarations. /* JavaScript Example (loose typing) */var a = 13; // Number declaration var b = “thirteen”; // String declaration
/* Java Example (strong typing) */int a = 13; // int declaration String b = “thirteen”; // String declaration
Notice that in the JavaScript example, both a and b are declared as type var. Please note, however, that this does not mean that they do not have a type, or even that they are of type “var”. Variables in JavaScript are typed, but that type is determined internally. In the above example, var a will be type Number and var b will be type String. These are two out of the three primitives in JavaScript, the third being Boolean.
Type coercion is a topic that is closely associated with loose typing. Since data types are managed internally, types are often converted internally as well. 7 + 7 + 7; // = 21 7 + 7 + “7”; // = 147 “7” + 7 + 7; // = 777 In the examples above, arithmetic is carried out as normal (left to right) until a String is encountered. From that point forward, all entities are converted to a String and then concatenated.
for more info : http://blog.jeremymartin.name/2008/03/understanding-loose-typing-in.html