Functions and Scope Flashcards
What are two ways to tell if something called myVar is an array?
- myVar instanceof Array;
2. Array.isArray(myVar); // not just isArray(var);
What does the toFixed() method do? (and on what type of value?)
It’s a method of the Number type and it returns a string with a specified number of decimal places. if n = 10.0092, n.toFixed(2) will return “10.01”;
var obj = new Object(); if (obj) { // will this take place? }
Yes, because objects convert to true when evaluated in Boolean expressions.
What is a function declaration compared to a function expression?
declaration: function myFunc(){ … }
expression: var myFunc = function() { … }
How could you get today’s day (e.g. “Monday”) from the date object?
You need to make an array with Sunday at 0 index and Saturday at 7 index.
Then use getDay() and find the day using your array.
Name the two RexEx methods that work with patterns:
exec() and test()
What is function declaration hoisting?
Before code begins running, the javascript engine “hoists” functions to the top of the execution context. That way, a function can be called even if it doesn’t appear in code until later. This only works with function declarations.
var arr1 = [1,2,3]; var arr2 = arr1; arr1 = null; what happens to arr2?
arr2 = [1,2,3];
arr1 and arr2 were both pointers to the same reference object (an array) but arr1 had it’s relationship that array broken.
function f1(){ // do something; } var f2 = f1; f1 = null; What happens if you call f2( ); ?
the original function will run.
When you set f1 to null, you broke the relationship between the pointer and the function but you didn’t change the function.
What does a for … in loop do and what does it look like?
It iterates over the enumerable properties in an object like this: for (var prop in obj) { // optionally, find only properties of this object, not the objects from which it inherits if (obj.hasOwnProperty(prop) { // do something
How do you access the function you are in (e.g. for a factorial)?
Use the name of the function. Arguments.caller is deprecated.
What are the string regular expression methods?
match()
search();
replace();
split();
Describe the apply() vs call() function
Both take two arguments, the value of “this” and some optional arguments to be acted upon. Apply takes an arguments object or an array; call takes a list of arguments: someFunc.call(this, arg1, arg2, etc)
How can a primitive value like “cats” can have access to methods, e.g. “cats”.substring(1,3) or “cats”.split(‘’);
Every time a primitive value is read, an object of the corresponding primitive wrapper type is created. This wrapper allows access to methods like substring() but it then is immediately destroyed.
var s = String; typeof s will be what?
“function”
string.replace() method take what argument(s)?
the pattern to match against and the thing to replace it with. This second argument (the replacement text) can be a function which will apply further logic.
var s = new String(); typeof s will be what?
“object”
What is format for string matching methods?
stringName.match(regExPattern);
stringName.search(regExPattern);
What are two ways to access the Global Object?
- Use window (window.property or window.method);
2. Return it from an anonymous function: global = function(){ return this; }();
how do you find a random number between 1 and 10?
var rand = Math.floor(Math.random() * 10 + 1);
Can you immediately invoke a function declaration as well as a function expression?
You have to wrap the function in parentheses and add () at the end:
(function(){ // do something }()). This works because parentheses can’t contain statements, so the parser knows to treat it like an expression.
use apply() to apply cat.getInfo() method to dog object.
cat.getInfo.apply(dog);
This will run cat’s getInfo() method but will do it using dog as the “this” object.
How does the bind() function work?
var bound = nameOfFunction.bind(nameOfObj); You need to tie it to a variable like this or the binding just gets lost. You can now say: bound(); // it will call nameOfFunction and the object we passed will be the "this" object.
When should you use typeof rather than instanceof operator?
Use typeof for testing simple build-in types, like strings, booleans, and numbers:
99.99 instanceof Number; // false
typeof 99.99 == ‘number’; //true
Also for functions and null.