JQuery Interview Questions Part - 8
Q 1. What are JQuery
utilities?
Ans 1. JQuery provides several utility methods that are helpful in completing
the desired programming tasks
Example –
Suppose, I
have a string - “ Hello
” but I need the text “Hello” without any spaces on its left or right,
then I can use trim() JQuery utility to remove any such spaces.
Q 2. How to use JQuery trim
utility method?
Ans 2. $.trim() is used to remove the leading and trailing whitespace.
Example –
$.trim(“ Hello
”) will return “Hello”
Q 3. How to use JQuery each
utility method?
Ans 3. $.each()
is used to iterate over arrays and objects. It can work on arrays and objects.
Example –
Usage with
arrays –
$.each([“JQuery”, “Javascript”, “AngularJS”],
function( index, value){
alert(“Element
at index: ” + index + “ is ” + value);
});
Result-
Element at index: 0 is JQuery
Element at index: 1 is Javascript
Element at index: 2 is AngularJS
Usage with
Objects -
$.each({Library: “JQuery”, Framework:
“AngularJS”}, function(key, value){
alert(“Key:
” + key + “ value: ” + value);
});
Result-
Key: Library value: JQuery
Key: Framework value: AngularJS
Q 4. How to use JQuery
inArray utility method?
Ans 4. $.inArray()
is used to Returns a value's index in an array, or -1 if the value is not in
the array.
var testArray = [“JQuery”, “Javascript”, “AngularJS”];
var returnedValue = $.inArray(“C++”, testArray);
alert(returnedValue);
Result: -1 (Because C++ does not exist in the array)
If “C++” is replaced with “AngularJS” in the above statement, then result will be: 2
Q 5. How to use JQuery
extend utility method?
Ans 5. $.extend()
is used to change the properties of the first object using the properties of
subsequent objects.
EXAMPLE –
var testObject1 = { Language: “C#”, Library:
“JQuery”};
var testObject2 = { Language: “Java”, Library:
“JQuery”};
var testObject3 = $.extend( testObject1,
testObject2);
alert(testObject1.Language);
Result – C#
alert(testObject3.Language);
Result - Java
Q 6. How to use JQuery
proxy utility method?
Ans 6. $.proxy()
is used to return a function that will always run in the provided scope i.e. it
relates a general function to a specific object.
EXAMPLE –
var testFunction = function(){
alert(this);
}
}
var testObject = {Library: “JQuery”, Language:
“C#”};
testFunction();
Result:
[object Window]
var proxy = $.proxy(testFunction, testObject);
proxy();
Result:
[object Object]
No comments:
Post a Comment