Javascript Weird Parts

¡Supera tus tareas y exámenes ahora con Quizwiz!

What allows blockscoping?

"Let"

What are the differences between Object Literal Notation and JSON?

1. All properties must be wrapped in quotes { "firstName" : "Mary", "isAProgrammer", true } ALL JSON is valid javascript but not all javascript is valid JSON. JSON has stricter rules so it can be parsed with other languages. JSON is not actually a part of JS but just modeled off of JS's object literal notation to keep the code light

Keep in mind coercion... What do these evaluate to? 1. false == 0 2. a = true; a > 0 3. null < 1 4. null == 0

1. true 2. true 3. true 4. false - there are some exceptions where things don't coerce as expected. This is one of those examples because null coerces to 0 in many cases

if var a = 3; var b = a; a = 5; what will this log? console.log(b);

3. primitives are stored by value and changing one after another has been set by it will not change the newly set variable. Objects are by reference (this includes functions because functions are objects).

What is the inside of a { } called?

A Block.

What is object literal notation?

A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces.

What is a namespace?

A container for variables and functions - typically used to keep variables and functions with the same name separate.

What is an "Expression"?

A unit of code that results in a value. It doesn't have to save to a variable.

Why does var number = 3 < 2 < 1 return true?

Because associativity (order of operations which can be found on MDN as "Javascript Operator Precedence") is left to right for the less than sign which in effect makes 3 < 2 < 1 turn in to false < 1 which turns into 0 < 1 which is true. False, when coerced into a number, equals 0. True turns to 1.

Why do people sometimes set 'this' to var self = this; ?

Because if you make inner functions and reference this it will break out of the object. Setting self = this when entering the object will make self always reference the "this" that you are trying to target, being the object that it lives inside. This is to get around the this issue that a lot of JS developers believe to be a bug You can get around this whole issue and needing to set this to self by using the let keyword

What are objects in JS?

Collections of name/value pairs. Can contain properties (primitives), objects (just another property) and methods (functions)

What are first class functions?

First class functions are a description of ALL JS functions. Everything you can do with other types, you can do with functions. functions ARE objects in JS. Assign them to variables, pass them around, create them on the fly.

Is this a function statement or a function expression? function greet(){ console.log("hi"); }

Function Statement.

What is function overloading?

Function overloading (also method overloading) is a programming concept that allows programmers to define two or more functions with the same name. JS DOES NOT HAVE FUNCTION OVERLOADING because functions are objects

How do the event queue and execution stack interact?

If an event takes place its related function (if it has one) goes on top of the event stack. The javascript engine will ONLY PULL FROM THE EVENT QUEUE ONCE THE EXECUTION STACK IS EMPTY

How does the following function work? function(name){ name = name || "Judd"; console.log("Hello " + name); }

If the name argument is left empty, it will be "undefined" and coerce to "false". Therefore the or operator will return a default name, "Judd", which coerces to true over name. If name is defined, since the or operator will coerce the new name and default name, it will take the first name and log it.

What does "IIFE" stand for?

Immediately Invoked Function Expression (called Iffys") You can create these like this: var greeting = function(name){ console.log("Hello " + name); }(); You can also pass parameters to the IIFE as follows: var greeting = function(name){ console.log("Hello " + name); }('John'); The classic use of these in almost every major framework looks like this: (function(argument){ inside function stuff })("passedArgument"); The parenthesis around the function tricks the parser into making it not throw and error and then it immediately runs this code on the fly.

What is dynamic typing?

It means that in JS you don't declare your types but JS figures it out as your JS is running

What would happen here? function getPerson(){ return { firstName: "tony" } } console.log(getPerson());

It would return undefined because the syntax parser would insert a semicolon after the return statement since there is a carriage return (or line break to the next line) just changing it to return { (on the same line) would prevent this from happening and would return the expected result

What native functionality is built IN to JS for JSON?

JSON.stringify and JSON.parse. Stringify takes a JSON object and turns it into a string. Parse takes the string and turns it into a Javascript Object. This takes you from JSON object to Javascript object.

What does JSON stand for?

Javascript Object Notation

What does Hoisting actually do?

Javascript engine runs through code and sets up memory space for functions and variables. Functions are set up in their entirety while variables are initially set as undefined.

How does javascript handle including scripts like the following? <body> <script src="lib1.js"></script> <script src="lib1.js"></script> <script src="app.js"></script> Other html goes here.... </body>

Javascript takes the code from these files and quite literally stacks them on top of each other like they were one file. So if Lib1 and Lib2 both have the variable "libraryName" there will be conflict.. they will both be global variables sitting inside the global execution context

What does let do?

Just like a normal variable it is set to undefined when the memory space is created (Global Execution Context) but the javascript engine will not allow the variable to be used for anything other than the set value and will throw an error if you try.

How many types of the "Number" type are there?

Just one. Number is, under the hood, the only primitive type for a number and is always floating point(contains decimals), though you can fake it.

What is "mutate" in JS?

Literally just means to change something. Immutable means that it cannot be changed.

What are the special properties of a JS function (functions are objects)?

Name and Code. Functions CAN have names in JS but do not have to. They can be anonymous. Code is where your actual code that you have written lives. It is a property AND is invocable ();

Why is Javascript called Javascript?

Needed to have developers to keep it alive - called in Javascript to attract java developers even though its not like java

Does function overloading exist in JS?

No it does not. A lot of other languages do. There are a number of ways to deal with this.

Does JS have native namespaces?

No. But we can fake it.

In JS do you need to specify the correct amount of arguments to a function to run it?

No. This is something strange about JS that isn't usually the case in other languages.

Are "undefined" and "Error: not defined" the same thing?

No. Undefined is a special value in JS when a variable is declared and not set - not set is when javascript is when trying to use something that doesn't exist (Is not in memory).

What is synchronous?

Only one at a time. Kind of like single threaded

What is single threaded execution?

Only one command is executed at a time.

What are prototypes?

Prototypes are for example: function Person(firstName, LastName) { this.firstName = firstName; this.lastName = lastName; console.log("This function is invoked"); } You can create objects with these like so: var john = new Person('John', 'Doe'); You can add methods and properties via john.prototype.methodOrProperty = function(){ }

What is "Invocation"?

Simply running a function. or "invoking" it.

What is a syntax parser?

This is the intermediary that is taking the code that your write and translating it to what the computer can understand. This is being handled, for example, by the javascript engine located in the browser. YOUR CODE is not what is exactly being executed. It is what it is being translated to The syntax parser CAN even make changes to your code

What is the "Execution Stack"?

This is the, just how you'd imagine it, stack of "Execution Contexts" that the javascript engine is executing from top to bottom in a single threaded manner.

What does Undefined and Null coerce to with the Number() function?

Undefined coerces to NaN (not a primitive type but can be treated as such). Null coerces to 0.

What are the 6 primitive types in JS?

Undefined, Null, Boolean, String, Number, Symbol (new in ECMA 6)

What is the "Global Execution Context"?

When the initial memory space is allocated and window is set up with the "global object" being the window and "this". Attaches all variables as undefined and functions. Execution context is argued to just be called "Scope" in JS

When would you access properties in an object with Object["propertName"] instead of Object.propertyName?

When you have some type of dynamic string like [variableName] or ["a property with spaces"]

What is the "Variable Environment"?

Where in the memory the variables live and relate to each other.

How does javascript output function.arguments?

With italicized []- this is considered "array like" which is almost like an array but lacks as many features.

Data was sent over the internet in various ways in the early days, what was the standard before JSON?

XML, which looked like this: <object> <firstname>Mary</firstname> <isAProgrammer>true</isAProgrammer> </object>

Can you ever set a variable to undefined? Should you?

Yes, you can. No - you should not.

in the code: function a() { b(); var c; } function b() { var d; } a(); var d = "hello"; in the global execution stack starting at "a();" will executing B(); be put on top of the global execution stack before var d is set?

Yes. Because JS is single threaded, once function A is invoked, b() will be invoked inside of function a, along with any other code, before it goes to the line below where a is invoked.

One thing people are surprised about when they see JS

You can attach properties and methods to a function. Because they are just objects with some special properties

When would you use undefined and null?

You should always leave undefined for the engine to relay to you for errors. You may use Null if you want to set a variable to "nothing". They are both primitive types.

What is a property?

a primitive inside of an object.

What will myVar equal when b() is invoked in the following code? function b(){ var myVar; } function a(){ var myVar = 2; b(); } var myVar = 1; a(); What if you took the declaration ' var myVar; ' out of b()? What if you took the declaration ' var myVar;' out of b() and moved the global declaration: 'var myVar = 1' below the invocation of a()?

b will be undefined because each variable instance of myVar has its own "Variable Environment". If you took the declaration out of b() it would equal 1 which is defined in the global scope. if you moved the declaration 'var myVar = 1' below the invocation of a(), logging myVar in b() would be undefined.

What is coercion?

converting a value from one type to another. Ex. var answer = 1 + "2"; answer ends up being 12 and converts 1 into a string along with the 2

What is this code doing? arr[3]();

finding the 4th item in an array, which is presumed to be a function, and running it.

in ES6, how do you set a default argument? How do you do it for ES5 or older browsers?

function checkitout(argu1, argu2, argu3 = "cat"); ES5 and older browsers function checkitout(argu1, argu2, argu3){ argu3 = argu3 || "cat"; }

what will this script return? greet(); function greet() { console.log("hi"); } anonymousGreet(); var anonymousGreet = function(){ console.log("hi"); }

hi uncaught TypeError: undefined is not a function when the script is run it sees a function and puts it into memory automatically. When it sees var anonymousGreet it sets it the var to undefined before it executes the script and encounters what the variable is set to.

What will this eventually do in JS? function(argu1, argu2, argu3, ...other){ }

if you overload a function with more arguments than specified, adding "...other" as an argument in the setup will take all overloaded arguments and place them in here in an array.

By Value and By Reference Problem. If var cat = { name: "Felix" } var b = cat; cat = { name: "Mojo" } console.log(cat) console.log(b);

logging cat will log: {name: "Mojo"} logging b will log: { name: "Felix"} this is because the = operator will set a new space in memory. Since var b was referencing the initial memory space held by var cat, it will remain and b will still equal that object. Setting cat to a different object will still change the first variable.

What happens here? var c = { name: "the c object", log: function(){ this.name = "updated name!"; console.log(this); } } c.log();

logs Object{ name: "updated name!", log:function} "this" is attached to the c object therefore references it.

What happens in this script? function a(){ console.log(this); this.newVariable = "hello cats!"; } console.log(newVariable);

logs Window object then logs "hello cats!" - because this is now attached to the global window object.

How can we fake a name space?

make an object (var english = {}) and put things inside of it. english.greet = "Hello!"

What is asynchronous?

means "more than one at a time"

what is __proto__

shows that arrays, objects, whatever have a base prototype where basic methods live

What is "overloading" a function?

something

What is considered "white space"

spaces, tabs, carriage returns

what will a script that looks like this log? function a(){ console.log(this); } var b = function(){ console.log(this); }

the Window object. again, the Window object.

What will happen here? var c = { name: "the c object", log: function(){ this.name = "updated c object"; console.log(this); var setname = function(newname){ this.name = newname; } setname('Updated again! the c object'); console.log(this); } }

the first console.log will log the c object with the name "the c object" with a function of log. the second console.log(this) will show that we've added a name property to the window object set to "Updated again! the c object'. A function inside of a function using the "this" keyword has broken out of the object and is now referencing the global object. MANY CONSIDER THIS A BUG but this is how it works for now.

In the function: if (a === 3){ }; What is the statement and what is the expression?

the if itself is the statement ( if (a===3){ ) because it does not return a value. a===3 is an expression.

What are arguments?

the parameters that you pass a function. Javascript gives you a keyword of the same name which contains all of them.

What will the console log when you enter: undefined || "hi"? 0 || 1? null || "hello"? "" || "hello"

undefined || "hi" will return "hi" 0 || 1 returns 1 null || "hello" returns "hello" "" || "hello" returns "hello" because these coerce to true where their counterparts convert to false

What is the array literal syntax?

var array = [];

How do you create an object?

var person = { } or var person = new Object(); The first way is preferred.

How can you write multiple variables on a single line?

var person, place, thing;

What is the difference of "by value" and "by reference"?

when locating something in memory by value it has its own value in memory. By reference is when an variable is pointing to the same object as something else. if a = 3; and b = a; there are two places in memory where a = 3 and b = 3; when the object Cat exists and Felix = Cat; There is only one instance of Cat in memory and both point to the same object. ALL objects work by reference whereas primitives work by value;

Say that we are including multiple javascript files which are appending global variables. How can we make sure that we don't overwrite an existing variable?

window.variableName = window.variableName || newVariableValue


Conjuntos de estudio relacionados

Sexuality-human papilloma virus (HPV)

View Set

Review 8.03 Classes, Links, Tables Code

View Set

6P Historia. ISRAEL Y PALESTINA, y REINO UNIDO

View Set

Small Business Management Chapter 3 Test (for midterm)

View Set

Human A & P II Lecture: Chapter 16

View Set

Chapter 7 - Axial and Appendicular

View Set