E questions Flashcards
(74 cards)
What is event bubbling?
when an event which triggers an event handler located on a child component also triggers an event handler on the parent component - the opposite of this is event capturing
For example, if the user clicks a hyperlink, that click event would pass through the <p> element containing the link, the </p> element, the element, and the document node.
You could put an alert on each one of these elements and the first alert would be on the link, then on the p, then on the body.
*What is the difference between let, const, and var?
var - the option before ES6, either local or global depending on where it’s declared, is hoisted (initialized as undefined), can be accidentally or unintentionally redefined
let - block scoped, cannot be redeclared in the same block but can be updated, can be redeclared in different blocks (will act as different variables)
is hoisted but not initialized
const - maintains constant value, block scoped, cannot be updated or redeclared but slightly different when it comes to objects where you can update individual key/values but not the entire object
var declarations are globally scoped or function scoped while let and const are block scoped.
var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared.
They are all hoisted to the top of their scope. But while var variables are initialized with undefined, let and const variables are not initialized.
While var and let can be declared without being initialized, const must be initialized during declaration
What is variable scope?
scope is the range in which a variable is accessible in the context of the code. Local variables are defined within a function, global variables are declared outside of a function. block scope is a type of local scope and is anything within curly brackets like if etc does not create a new scope (only const and let). Lexical scoping is when a nested function has access to the outer functions variables, javascript uses this. Does not work the other way around.
What is a closure?
A function within another function, the inner function is the closure and is usually returned. Benefits - make it easier to control side effects, make it easier to create private variables.
PHP what is private vs protected?
A private constant, property, or method can only be accessed from within the class that defines it. A protected constant, property or method can only be accesed from within the class that defines it, or a descendant (inherting and parent) of that class. A public constant, property, or method can be accessed from anywhere.
*Difference between == and ===
both are comparison operators
=== compares both type and value, strict, does not try to convert values
== only compares value, not type. converts both to the same type to compare
*difference between let and const
let can reassign as many times are you want and/or change the type
const after the first assignment of value, you cannot reassign or modify - better to use const so you don’t unintentionally reassign a variable. you can change things within a const object or array, but you can’t change it completely.
*what is the use of arrow functions?
more concise way to write a function expression
traditional expression function (a, b){ return a + b + 100; }
arrow function (a, b) => a + b + 100;
arrow functions cannot bind or use ‘this’
Difference between function declaration and function expression
function declaration- alert(foo()); // Alerts 5. Declarations are loaded before any code can run. function foo() { return 5; } has global scope, cannot pass into another function
function expression - alert(foo()); // ERROR! foo wasn't loaded yet var foo = function() { return 5; }
anonymous function stored to a variable - has variable/limited scope, can only be called after creation, can pass into another function
what is a promise and why do we use it?
Promises are JavaScript objects that represent an eventual completion or failure of an asynchronous operation. A promise is a returned object where you attach callbacks, instead of passing callbacks into a function. the place where you attach the callback after a successful completion of a task is called, . then().
what is a promise and why do we use it?
Promises are JavaScript objects that represent an eventual completion or failure of an asynchronous operation. A promise is a returned object where you attach callbacks, instead of passing callbacks into a function. the place where you attach the callback after a successful completion of a task is called, . then().
what is setTimeOut()
When you add setTimeOut it will make a function asynchronous - so it will execute after everything on a stack completes before it runs, even if the timing is set to 0
What is a closure and how do you use it?
A closure can be any function within another function, it’s key characteristic is that it has access to the scope of the parent function including it’s variables and method. Also has the potential for a function to have private variables. Positive - allows you to separate concerns very cleanly. Can also use it to implement throttling and debouncing.
const add = (function () { let counter = 0; return function () { counter += 1; return counter } })();
add();
add();
add();
// the counter is now 3
If a function returns another function, the returned functions also hold on to the original variables from the first function. It is a way of keeping access to variables in a function after that function as returned. Disadvantages - variables used by closure will not be garbage collected, memory snapshot of the application will be increased if closures are not used properly.
What are Javascript events
An event is a process that allows you to react when certain things happen on a page. event -> action
inline events -> onclick
addEventListener -> most imporant
arrow function vs regular function
// Regular Function Syntax ES5: var add = function(x, y) { return x + y; }; // Arrow function ES6 let add = (x, y) => { return x + y };
Function expressions are best for object methods. Arrow functions are best for callbacks or methods like map, reduce, or forEach.
Arrow functions - lexical scoping
**PHP - What is the difference between Echo and Print
both are PHP statements used to display data output to the screen.
- echo does not return a value whereas print does return a value of 1 (which allows print to be used in expressions) echo is faster than print
- echo can accept multiple parameters, while print can only take in a single argument.
print_r works for arrays where echo only prints “array”
PHP difference between $var and $$var
$var is a simple variable
$$var is a reference variable
*PHP what are the differences between require and include.
Require and include both are used to include PHP script from one file to another file.
Require says a page must be developed - if it’s missing or has a wrong name there is a fatal error which stops the execution of the script.
A good case for require is in cases where a database connection file is involved and helps alleviate the possibility of multiple instances of the same file being included several times.
Include also expects a page to be developed, but will show a warning error and then continue to run the script.
include_once()
include
require_once()
require
What are the different types of errors in PHP
- notice error - when a variable is undefined
- warning - gives permission to execute the script but shows a warning message meaning a potential error is present.
- fatal - critical error, stops the execution of the script
PHP What is the difference between unlink and unset
Unlink() is used to delete files while Unset() makes a variable undefined.
PHP How can we get the properties of the browser?
$_SERVER[‘HTTP_USER_AGENT’] global variable is used to access browser properties
What is the maximum file size that can be uploaded in PHP and how can we increase it?
By default the max size of a file that can be uploaded is 2MB. You can change the max uploaded size value inside of php.ini file and set the value upload_max_filesize = 5M and restart all services.
PHP How do we get the value of the current session id?
The session_id() function is used to get the current session id. It returns the session id for the current session.
Is multiple inheritance supported in PHP?
No, multiple inheritance is not supported.