JavaScript Questions Answers 09092015
JavaScript Questions Answers 09092015
Anonymous function is just a function without name.It is used for constructing the result
of a higher-order function[ is a function that does at least one of the following:takes one
or more functions as arguments,returns a function as its result.] that needs to return a
function.
ex:
fnAnonymous(); //hi
1.If no name is needed because the function is only ever called in one place, then why
add a name to whatever namespace you're in.
2.Anonymous functions are declared inline and inline functions have advantages in that
they can access variables in the parent scopes. Yes, you can put a name on an anonymous
function, but that's usually pointless if it's declared inline. So inline has a significant
advantage and if you're doing inline, there's little reason to put a name on it.
3.The code seems more self-contained and readable when handlers are defined right
inside the code that's calling them. You can read the code in almost sequential fashion
rather than having to go find the function with that name.
Every object descended from Object inherits the hasOwnProperty method. This method
can be used to determine whether an object has the specified property as a direct property
of that object; unlike the in operator, this method does not check down the object's
prototype chain. hasOwnProperty() is a normal Javascript function that takes a string
argument.
ex:
function Shape() {
this.name = "Generic";
this.draw = function() {
return "Drawing " + this.name + " Shape";
};
}
function welcomeMessage()
{
var shape1 = new Shape();
alert(shape1.hasOwnProperty("name")); //this is returning true
}
Q3)JavaScript What is a polyfill? When might you use one? What is an example of a
popular polyfill?
A polyfill is a browser fallback, made in javascript, that allows functionality you
expect to work in modern browsers to work in older browsers. Ie to support canvas (an
html5 feature) in older browsers.
example
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].filter(isEven);
// returns [2, 4, 6, 8, 10]
Q4)JavaScript While inside a function definition, what is the difference these two
statements?
x = 1;
var x = 1;
Variables declared outside a function become GLOBAL, and all scripts and functions on
the web page can access it.
If you declare a variable, without using "var", the variable always becomes GLOBAL.
2) "==" operator is known as type coercion operator and anytime if both values are same
and compared using ==operator, type coercion happens. On the other hand === is known
as strictly equality operator. It's much similar Java's equality operator (==), which gives
compilation error if you compare two variables, whose types are not compatible to each
other. In fact, you should always use "===" operator for comparing variables or just for
any comparison.
Event bubbling and capturing are two ways of event propagation in the HTML DOM
API, when an event occurs in an element inside another element, and both elements have
registered a handle for that event. The event propagation mode determines in which order
the elements receive the event.
With bubbling, the event is first captured and handled by the innermost element and then
propagated to outer elements.
With capturing, the event is first captured by the outermost element and propagated to the
inner elements.
Capturing is also called "trickling", which helps remember the propagation order:
Only the bubbling model is supported by all major browsers. If you are going to use
event capturing anyway, you need to handle event bubbling for IE. So event bubbling is
preferable.
--------
Q7)CSS Describe a couple of different ways to overlay some text on an image
html page
<div class="texthover"><br />
<img
src="http://api.ning.com/files/zNSLWuvsJ1IsNDWk68XplsZ6uepRjnDFBhg9Q5*DJd9n
krPgTeTdhz3Bwkp5FOy72uYwk*a6oW0hpwjWf4T90-Fvx-rf0Qrj/religion.jpg" />
<div class="overlay"><br />
<span>This is some text I want to display</span></div>
</div>
css page
DIV.texthover {
width:100%;
display:block;
position:relative;
text-align: center!important;}
DIV.texthover .overlay {
position:absolute;
top:45%;
width:0;
width:100%;
height:50%;
padding:10px;
background-color:rgba(0, 0, 0, 0.3);
display:none;
}
DIV.texthover:hover .overlay {
display:block;
}
Q8)CSS List as many values for the “display” property that you can remember
inline,block,flex,inline-table,initial,table-cell,table-column,table-row
Inline elements:
respect left & right margins and padding, but not top & bottom
cannot have a width and height set
allow other elements to sit to their left and right.
Inline-block elements:
---
Q10)jQuery Explain “chaining”
In chaining parent function/method returns an object which is then used by the child
function/method, and things go on such a way. In short the jQuery or $ returns itself (an
object) which allows the chaining.Chaining allows us to run multiple jQuery methods (on
the same element) within a single statement.
exmple: $("#p1").css("color", "red").slideUp(2000).slideDown(2000);
jQuery selectors are used to "find" (or select) HTML elements based on their id, classes,
types, attributes, values of attributes and much more. It's based on the existing CSS
Selectors, and in addition, it has some own custom selectors.
All selectors in jQuery start with the dollar sign and parentheses: $().
deferred.resolve("hello world");
deferred.done(function(value) {
alert(value);
});
----
The data-* attributes is used to store custom data private to the page or application.
The data-* attributes consist of two parts:
The attribute name should not contain any uppercase letters, and must be at least one
character long after the prefix "data-"
ex:
<article
id="electriccars"
data-columns="3"
data-index-number="12314"
data-parent="cars">
</article>
progress - The progress element is used to represent the progress of a task. It supports
both indeterminate situations as well as determinate situations.
Ruby - The ruby element is used to support one or more spans of content to be marked
with ruby annotations.
rt - The rt element is used to mark the ruby text component for a ruby annotation.
rp - The rp element is used to provide parenthesis around a ruby text component of a ruby
annotation.
Section - The section element is used to represent a generic section of a document or
application, usually containing a heading. E.g. chapters in a book.
Source - The source element is used to specify to alternate media resources for media
elements. It is not supposed to be dynamically modified, as that will have no effect.
Summary- The summary element is an interactive element which is used to represent a
summary, or caption for a details element.
Time - The time element represents datetime attribute contents in a machine readable
form (limited to various kinds of dates, times, time-zone offsets, and durations).
Video - The video element is a type of media element used to represent a video stream.
wbr - The wbr element is used to represent a line break in a web page.
----
Q17)Security What is CSRF and how do you prevent it?
Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a malicious Web
site, email, blog, instant message, or program causes a user’s Web browser to perform an
unwanted action on a trusted site for which the user is currently authenticated. The impact
of a successful cross-site request forgery attack is limited to the capabilities exposed by
the vulnerable application.
To implement this, you have to include a few logic layers in your application.
1.Include a hidden form field on all forms of your site which are required when a user is
logged in. For example purposes, let's call it the nonce.
2.Include the nonce value in the users session variables, so that they have a copy.
3.After every POST or GET evaluation you want to protect, make sure that the nonce
from the browser (session) matches the nonce from the form (GET/POST value)
4.If they do not match, then the page may have been submitted fraudulently
5.The nonce may be generated new for each form, or stored server side in a database. Be
aware that if it is generated new each time, you may face issues with users back button
functionality, or multiple tabs, although this ius less work then storing it server side.
Q19)Accessibility If I have a <div> that behaves like a button (i.e.: has a click event
handler), how do I indicate that functionality to a screen reader?
The aria-describedby attribute is used to indicate the IDs of the elements that describe the
object. It is used to establish a relationship between widgets or groups and text that
described them.
ex:
...