JavaScript 3 Flashcards
What is a three step ‘algorithm’ for creating a JavaScript object?
First, write a constructor function that sets the instance properties of the new object.
Second, define instance methods on the prototype object of the constructor.
Third, define class fields and class properties on the constructor itself.
Is there a JavaScript equivalent of the ‘final’ keyword in Java that declares instance variables as constants?
No
If I want to emulate a static function on a JavaScript object, where do I declare the function?
On the constructor
Constructor.myfunc = function() {};
This would allow you to call the function without instantiating an object - you would normally not use the ‘this’ keyword in a method like this.
The expression ‘o instanceof c’ returns true if…
the object o inherits from c.prototype - the relationship does not have to be direct.
In JavaScript - Constructors act as the public identity, but the formal identity resides with the…
‘prototype’.
Does the following function declaration have a constructor name? var Complex = function (x) { .... };
No
Does the following function declaration have a constructor name?
var Complex = function Complex(x) { …. };
Yes - it will be Complex.
A set is…
A data structure that holds a collection of values with no duplicate values.
The job of toLocaleString is to…
Convert a string in a locale sensitive method
The job of valueOf is to…
Convert the object into a primitive value when used in a numeric context.
What method invokes the toJSON method on an object?
JSON.stringify()
JavaScript equality operators evaluate object equality by…
Reference not the values - i.e. it checks that they point to the same object.
How should you implement equality testing in JavaScript?
Use a similar method to Java. Define methods such as “equals” - which take one object as an argument and return true or false depending on the result.
If the < or <= operators are called on an object, what function is called to perform the test?
The valueOf() method is called to see if it can compare using the result of that method.
What method should be used instead of using the less than / greater than operators?
Using Java conventions - create a method called compareTo. Then pass it the object we are comparing to. Use a test against 0 to determine the result.
i.e.
a b - becomes a.compareTo(b) > 0;
a != b - becomes a.compareTo(b) != 0;
B is a subclass of A. If method B.method wants to call the overridden method A.method, what is this called?
Method chaining
B is a subclass of A. If we want to call the overridden constructor A from B - what is this called?
Constructor chaining
The key to subclassing in JavaScript is the proper initialisation of the…
prototype object - if B extends A, then B.prototype must be an heir of A.prototype
What two lines of code are the most important part of subclassing in JavaScript?
B.prototype = Object.create(A.prototype); B.prototype.constructor = B; // Override the inherited constructor property
Without these lines - any objects created are simply subclasses of Object.
How would you get the width of a window?
window. innerWidth
window. outerWidth
You may have to take into account the scrollbar though
Is the use of querySelector and querySelectorAll okay?
Yes, most modern browsers support it - there is an IOS8 bug which prevents some selector types working (i.e. a + p) - and IE 8 support is spotty (but should be okay for basic selectors).
Write a simple query that returns all elements with the class “foobar”…
var allFoobars = document.querySelectorAll(“.foobar”);
What selector syntax is used with querySelector and querySelectorAll?
Basically the same as CSS - with the exception of older browsers that may only have partial support (i.e. IE) - it’s pretty much the same shit. NOTE: this is because IE 8 does not support CS3, and only parts of CS2.1
If you use querySelector on a class, and there are multiple elements with this class, which one does it return?
The first one it finds.