Javascript Flashcards

1
Q

Method for finding elements without using getElementById

A

document.querySelector(“#id”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Property that represents the HTML text between an elements opening / closing tag

A

element.innerHTML = “text”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What version of querySelector finds all of the elements with a given tag?

A

document.querySelectorAll(“.class”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the traditional way of getting an element via ID?

A

document.getElementById(“myid”);

NOTE no # for the id

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you change the CSS style of an element?

A

element.style.color = “red”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write code to create a P element and append it to the end of the body element.

A

var p = document.createElement(“p”);

document. body.appendChild(p);
p. innerHTML = “Hello World!”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you set an attribute on an element that you have created through code?

A

p.setAttribute(“id”, “example”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you remove an element from a page?

A

p.parentNode.removeChild(p);

This will effectively remove itself (but note that it has to go through the convoluted path of getting it’s parent then having parent delete the node)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write code to set a “click” handler on a button element?

A
var but = document.querySelector("button");
but.addEventListener("click", clickHandler, false);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you remove an event listener?

A

but.removeEventListener(“click”, clickHandler, false);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you produce a random number between 1 and 10?

A
var num = Math.floor((Math.random()*10) + 1);
or
var num = Math.ceil(Math.random() * 10);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Convert a string into an int.

A

var num = parseInt(input.value);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you determine if a value is a string?

A

if (isNaN(variable)) { // Do something appropriate }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you disable a button in code?

A
var button = document.querySelector("button");
button.disabled = true;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How could you use Math.random() to give a random true / false result?

A
if (Math.round(Math.random() === 1) {
  // True
} else {
  // False
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How would you create a random number between 10 and 25?

A

Math.floor(Math.random() * 16) + 10;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is some limitations of an object literal?

A

It is a singleton and also cannot implement the concepts of encapsulation, and inheritance. It also doesn’t require the use of new to create it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the basic difference in implementation for Object Literal and an Object in javascript?

A
Object Literal:
var shape = {
   height: 10,
   width: 10,
   area: function() { // stuff }
}
Object (created by using new)
function Shape() {
  this.height = 10;
  this.width = 10;
  this.area = function() { // stuff };
};
19
Q

What is the equivalent of a class in Javascript?

A

The constructor function

20
Q

When in the global scope, what does “this” point to?

A

The window object.

21
Q

How is a public variable implemented in Javascript?

A

By declaring the member with the “this” keyword - it becomes available through the object

22
Q

How is a private variable implemented in Javascript?

A

By declaring the member with the “var” keyword. It can no longer be accessed outside the object and is local to it.

23
Q

How do you declare an object that takes parameters?

A
function Shape(w, h) {
    var height = h;
    var width = w;
this.getArea = function() {
   return h * w;
} }

var myShape = new Shape(10, 10);

24
Q

How do you set default values for an objects parameters?

A
function Shape(w, h) {
   var height = h || 4;
   var width = w || 10;
....
25
Q

What is the value of an object parameter that is not passed when creating the object?

A

undefined

26
Q

Is it possible to override methods in JavaScript?

A

No

27
Q

Does overloading occur if two functions in an object have the same name?

A

No - the last declared one overwrites the first.

28
Q

How can you declare a function that can take variable arguments?

A

this.getArea = function() {
console.log(arguments);
}

29
Q

How can you add a new entity to an existing object?

A
var foo = new Foobar();
foo.num = 10;

NOTE: This only adds the entity num to this instance of the object.

30
Q

What is the difference between class based OO languages and JavaScript?

A

JavaScript is prototype based, which means instead of using classes to make objects, it uses objects to create objects.

31
Q

What does ‘DOMContentLoaded’ event fire on?

A

When the page is fully loaded (but doesn’t wait for the style sheets or images).

document.addEventListener(‘DOMContentLoaded’, callable);

32
Q

What event can be used to determine if an image (game asset) has been loaded?

A

image.onload = function() {
};

// You can use this to decrement a counter that is keeping track of the number of assets loading… when it is 0 - you can start the game.

33
Q

What does the following code do??

window.requestAnimationFrame = window.requestAnimationFrame ||
window.webkitAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000/60);
};

A

Because some browsers don’t support requestAnimationFrame - it overwrites the window method requestAnimationFrame with a pointer to the method that DOES exist. Whether that be requestAnimationFrame, or msAnimationFrame. This allows us to continue using requestAnimationFrame name while it points to the most appropriate method for the given browser. Note - the last part of the conditional is a callback function that reverts to the method of simply calling the main game loop again.

34
Q

Is ‘window.requestAnimationFrame(callback)’ equivalent to ‘requestAnimationFrame(callback)’?

A

Yes, because window is the global container - you can access its members anywhere.

35
Q

What is the term for a javascript object?

A

Prototype

36
Q

Create a dog object with a function named bark.

A
function Dog {
}

Dog.prototype.bark = function() {
console.log(‘woof’);
};

var MyDog = new Dog();
MyDog.bark();
37
Q

What is a self invoking function?

A

A function that will call itself. It is created with the following syntax.

(function () {
    var x = "Hello!!";      // I will invoke myself
})();
38
Q

How do you simulate optional arguments in Javascript.

A

By testing each argument in the function body against undefined - you can determine if a value was passed. i.e.

function (var1, var2) {
   if (var1 === undefined) {
      var1 = VAR1_DEFAULT_VALUE;
   }
}
39
Q

In Javascripts inheritince model - does the ‘override’ keyword exist?

A

No - you simply redefined the method on the new object.

40
Q

How do you inherit from an existing object?

A
function newObject() {
}

newObject.prototype = new Object.create(OtherObject.prototype);

41
Q

Basic / primitive types in Javascript are pass by…

A

value

42
Q

Non-primitive types (such as objects) are pass by….

A

reference

43
Q

How do you call a function from the superclass (i.e. to call the parent version of an overridden function)?

A

I want to call the update method in the parent class:

childObject.prototype.update = function(myValue) { 
   parentObject.prototype.update.call(myValue);
   // Do other stuff
};