JavaScript Flashcards
What is the effect of using document.write() in the HTML body?
It inserts text into the document at that point.
What is the effect of using document.write() in a function?
In this case, document.write() is called after the document has loaded, and overwrites the entire document.
V8
Chrome’s JavaScript engine, written by Google
node.js: process
process takes the place of window in regular javascript, because node apps run on a server, not a browser window. Node allows you to write javascript in a console.
node: setTimeout and setInterval
Allow you to asynchronously schedule events. You never sleep in node.js
curl
library and command-line tool for fetching data using URLs over http, ftp, and many other protocols. Handles cookies, very powerful.
meta
HTML tag for providing metadata in key/value pairs. name=/content= pair used to specify generic metadata such as search terms (keywords), author, or description. Keys charset, http-equiv, and scheme(depr) offer other functionality.
http-equiv
Simulates an http header response with appropriate key/values. Key content-type specifies encoding (eg text/html), key refresh will reload a web page at intervals, key default-style will load CSS.
Where does javascript go in an html doc?
In the header or else linked in from an external file.
What do legal javascript variables start with?
$ _ or a letter. Numbers can follow.
What are the data types in javascript?
Numbers, strings, booleans, and objects. (Arrays are objects).
What is Duck Typing?
It is a form of dynamic typing. In duck typing, an object’s methods and properties, rather than inheritance, determine its semantics.
How do you add a method to an existing object?
person[‘getBirthday’] = function() {
return ‘July 18th’;
};
Are javascript types completely dynamic type-inferred?
Javascript types are dynamic. You can create a variable, assign a string, number, an object, then a different object. Run time errors will only be generated when you use the variable in a way that violates semantic rules for whatever its dynamic type happens to be.
What is the difference between “string” and ‘string’?
Nothing. Both forms work, and you can use one form to avoid escaping the other form ala:
“This isn’t hard.”
‘This is “hard”.’
What does javascript do if you use a variable which has not been previously declared?
Error. Stops execution.
How are javascript variables scoped?
Javascript variables are scoped to function, not block. Any extra ‘var’ declarations have no effect, where in other languages they may give you a new data store (such as declaring a variable in a loop).
What is the associativity of +?
Left-associative. This matters, for example, when your are stringing together strings and numbers. “3”+4+9 will result in 349, NOT 313 (4+9=13).
CSS: What is .foo { } ?
class selector. Targets all elements with class=’foo’
CSS: What is #foo { } ?
id selector. Targets all elements with id=’foo’. According to spec, there should be only one.
CSS: What is foo { } ?
element selector. Targets all HTML elements of type .
CSS: What is foo.goo { } ?
element by class selector. Targets all HTML elements of with class=’goo’.
How do you fetch an array of all elements by type?
getElementsByType(‘TAGTYPE’) returns an array
Under the W3C HTML DOM standard, what are five categories of nodes?
- Document node (entire file)
- Element node
- Text node (text inside elements)
- Attribute node
- Comment node
Which common HTML form element attribute indicates a function to be called when the element looses focus?
onBlur=”function()”
Which object contains information about the browser?
navigator
Which object and attribute gives you the browser?
navigator.appName
What is the difference between null and undefined?
undefined is a type and is assigned to variables that have been declared but not assigned (although it can be assigned literally). null is an object.
What is the alternative to foo.bar if bar is not a string literal and a legal javascript name?
foo[“bar”]
How are objects passed and handled?
By reference
What is happens with ‘foo.bar = “Hello”’ when bar is not a property of foo?
foo is augmented with bar.
What is a prototype?
An object linked to objects which provides properties that can be inherited.
What are the conditional statements?
if and switch
What are the looping statements?
for, while, and do
What are the disruptive statements?
break, return, and throw
What is label syntax for statements?
name : statement
What values are false?
false, null, undefined, ‘’, 0, NaN
What special properties do functions have?
constructor(code), this, arguments, outer context
What kind of function is this? var a = function (a,b) { return a+b }
anonymous
What are the four ways to invoke a function?
function, method, constructor, and apply
How is ‘this’ bound in a method?
‘this’ is bound to the object owning the method. It is bound late, at invocation time, which makes methods highly reusable.
How is ‘this’ bound in a function?
‘this’ is bound to the global object. This means that inner functions in methods cannot see the object (workaround: var that = this).
What type of inheritance does JavaScript use?
Prototypal Inheritance. Contrast with Classical-Inheritance. JS is class-free.
What happens when a function is invoked with new?
A new object is created. The object gets a hidden link to that function’s prototype property, ‘this’ is bound to the new object, and the meaning of ‘return’ for that function is temporarily changed.
What is a constructor?
By invocation, it is a function called with ‘new’. By declaration, it is simply a function intended to be used as a constructor. Because there are no restrictions on what can be called with ‘new’, it is important to maintain a convention of Capitalized Constructors.
What does ‘apply’ do?
Invokes a function with specified bindings for ‘this’ and arguments.
What is returned from a constructor invocation?
If the return statement specifies an object, that object is returned. Otherwise, ‘this’ is returned.
How can you make a method available to all objects?
Define the method on Object.prototype
How do we augment all objects so that foo.method is supported for any object foo?
Add the method to Object.prototype ala Object.prototype.method = function(){}. You can do the same with functions, arrays, regular expressions, strings, numbers, and booleans.