Ad. Javascript.info part 1 Flashcards
Advanced working with functions - Recursion and stack - Rest parameters and spread operator
An object is a ________________.
collection of properties and has a single prototype object.
The prototype of an object may be ______ or ______
either an object or the null value
A prototype of an object is referenced by the hidden internal _________ and property
[[Prototype]]
A finite chain of objects which is used to implement inheritance and shared properties
Prototype chain
code reuse stylistics is called the
class-based inheritance
is the actual object that is used in the lookup chain to resolve methods, etc.
__proto__ or [[prototype]]
is the object that is used to build __proto__ when you create an object with new:
prototype
If the property is not found after the whole prototype chain lookup, then ________ is returned.
undefined value
Object.prototype itself also has a __proto__, which is the final link of a chain and is set to______.
null
a constructor function does another useful thing — it automatically sets a ___________ for newly created objects.
prototype object
function Foo(y) { this.y = y; }
// inherited property "x" using constructor \_\_\_\_\_\_\_\_\_\_\_\_\_\_= 10;
Foo.prototype.x
every object has a prototype
TRUE / FALSE
TRUE
An object specifies its prototype via the internal property_________
[[Prototype]]
What constitute a prototype chain are the __proto__ pointing up the chain, and the objects pointed to by __proto__, such as going from foo.__proto__, going up to foo.__proto__.__proto__, and so forth, until _______
null is reached.
By default, JavaScript engine provides the Object() funtion and an anonymous object that can be referenced to via the ________
Object.prototype.
The Object.prototype object has many built-in properties such as toString(), valueOf(), etc. It also has a property named_______ that points back to the _________
constructor
Object() function
console.log(Object.prototype.constructor !== Object); //
false
console.log(Object.prototype.constructor === Object); //
true
let point = {
x: 10,
y: 20,
};
how many properties?
how many __proto__?
2 properties
1 __proto__
Every object, when is created, receives it’s ______
prototype
If the prototype is not set explicitly, objects receive _________as their inheritance object.
default prototype
// Base object. let point = { x: 10, y: 20, };
// Inherit from `point` object. let point3D = { z: 30, \_\_proto\_\_: point, };
inherited or own
console.log( point3D.x, // 10, point3D.y, // 20, point3D.z // 30, );
inherited
inherited
own
a delegation object used to implement prototype-based inheritance.
protoype
finite chain of objects used to implement inheritance and shared properties.
prototype chain
a mechanism used to resolve a property in the inheritance chain. The process happens at runtime, hence is also called dynamic dispatch.
delegation
delegation
a mechanism used to resolve a property in the inheritance chain. The process happens at runtime, hence is also called dynamic dispatch.
another name for delegation
dynamic dispatch.
let empty = {};
console.log(empty.x);
// undefined
To create a prototype-less dictionary, we have to explicitly set its prototype to ______
null
// Doesn't inherit from anything. let dict = \_\_\_\_\_\_\_\_\_\_
Object.create(null);
let dict = Object.create(null);
console.log(dict.toString); //
undefined
let protoA = {x: 10}; let protoB = {x: 20};
CREATE OBJECTC WRITE USING __PROTO__
console.log(objectC.x); // 10
let objectC = {__proto__: protoA};
let protoA = {x: 10}; let protoB = {x: 20};
CREATE OBJECTC WRITE USING .CREATE
console.log(objectC.x); // 10
let objectC = Object.create(protoA);
let protoA = {x: 10}; let protoB = {x: 20};
// Change the delegate to protoB DONT USE __PROTO__
console.log(objectC.x); // 20
Object.setPrototypeOf(objectC, protoB);
When several objects share the same initial state and behavior, they form a________
classification
________ is a formal abstract set which specifies initial state and behavior of its objects.
a class
var a = {}; Object.getPrototypeOf(a); // alert(a.\_\_proto\_\_);
[object Object]
a________ function creates objects, and also automatically sets the prototype for its newly created instances
constructor
Object is an unordered or ordered collection of key-value pairs.
unordered
function A() { this.x = 10; return [1, 2, 3]; }
var a = new A(); console.log(a.x, a);
undefined, [1, 2, 3]
function A() {} var a = new A(); console.log(a.constructor); // console.log(a.constructor === A); //
function A() {}, by delegation
true
As opposed to object literals, here, you define an object type without any specific values. Then, you create new object instances and populate each of them with different values.
constructor function
4 ways to create a object
with object literals
using a constructor function
with ECMAScript 6 classes
with the Object.create() method
The use of __proto__ is controversial, and has been discouraged.
TRUE / FALSE
TRUE
Allows the addition of properties to all objects of type Object.
Object.prototype
Object.prototype has a value of
1
What are the 4 characteristics of Protypal Inheritance in JavaScript
- Prototype Chain - behavior is delegated up the prototype chain if it is not available on the new object
- .prototype links the function to an object
- [[Prototype]] internally links one object to another (not public)
- __proto__ (dunder proto) “public property” a getter function which returns the prototypal linkage of THIS
The __________ is considered outdated and somewhat deprecated (in browser-only part of the JavaScript standard).
__proto__
Object.getPrototypeOf(obj) –
returns the [[Prototype]] of obj (same as __proto__ getter).
returns the [[Prototype]] of obj (same as __proto__ getter).
Object.getPrototypeOf(obj) –
Object.setPrototypeOf(obj, proto) –
sets the [[Prototype]] of obj to proto (same as __proto__ setter).
sets the [[Prototype]] of obj to proto (same as __proto__ setter).
Object.setPrototypeOf(obj, proto) –
In JavaScript, objects have a special HIDDEN property __________ (as named in the specification), that is either null or references another object.
[[Prototype]]
Object constructors are used to create an _________
instance of an object
in Javascript every function expression is a _____
constructor.
WHAT IS MISSING
let x = function ( ) { this.k = k; }
( k )
__proto___ means what?
parent or creator
Whats a prototype in Javascript?
a property that every function you create in javascript has, that points to an object
the entity behind almost all objects_______
Object.prototype.
Object.getPrototypeOf({}) ==Object.prototype);
true
Object.getPrototypeOf(Object.prototype)
null
The _____________ is an internal linkage that tight one object to another.
double bracket [[Prototype]]
A constructor is called with the _____ keyword.
new
A constructor names must start with a _________
capital letter
function Foo() {} var f = new Foo(); Object.getPrototypeOf(f) === Foo.prototype
TRUE / FALSE
TRUE
This is what type of of object
Function User(firstName, lastName, dateOfBirth) { this.firstName = firstName; this.lastName = lastName; this.dateOfBirth = dateOfBirth; }
var user001 = new User(“John”, “Smith”, 1985);
CONSTRUCTOR FUNCTIONS
This is what type of of object
var user001 = { firstName: "John", lastName: "Smith", dateOfBirth: 1985, getName: function(){ return "User's name: " + this.firstName + " " + this.lastName; } };
OBJECT LITERALS
This is what type of of object
class User { constructor(firstName, lastName, dateOfBirth) { this.firstName = firstName; this.lastName = lastName; this.dateOfBirth = dateOfBirth;
this.getName = function(){ return "User's name: " + this.firstName + " " + this.lastName; } } }
var user001 = new User(“John”, “Smith”, 1985);
ECMASCRIPT 6 CLASSES
This is what type of of object
var user001 = { firstName: "John", lastName: "Smith", dateOfBirth: 1985, getName: function(){ return "User's name: " + this.firstName + " " + this.lastName; } };
var user002 = Object.create(user001);
user002.firstName = “Jane”;
THE OBJECT.CREATE() METHOD
Make a blank object using the Object() constructor:
var d =
new Object();
Make a blank object using Object.create() method set to null
var d =
Object.create(null);
Make a blank object using the bracket’s syntactig
var d =
{}
You can find instanceof relationships by comparing a function’s prototype to an object’s ________ chain, and you can break these relationships by changing prototype.
__proto__
_________is a property of a Function object
prototype
Two ways to point to a prototype
__proto__
Object.getPrototypeOf( )
function Point(x, y) { this.x = x; this.y = y; } var myPoint = new Point();
WRITE THE REVERSE:
myPoint instanceof Point;
myPoint instanceof Object
myPoint.__proto__ == Point.prototype
myPoint.__proto__.__proto__ == Object.prototype
function Point(x, y) { this.x = x; this.y = y; } var myPoint = new Point();
WRITE THE REVERSE:
myPoint.__proto__ == Point.prototype
myPoint.__proto__.__proto__ == Object.prototype
myPoint instanceof Point;
myPoint instanceof Object
let f = new function () let o = new object () let a = new array (0
ALL in hertiate from:
object.prototype
Function objects inherit from ________
Function.prototype.
Funtion.protype inherits from
object.prototype
to look up the prototype chain we use _______
__proto__
with constructor functions we dont need the _____ keyword
return
function createCircle (radius){ return { radius, draw () { console.log('draw'); } }; }
What type of function?
factory
____________ is the default toString representation of an object in javascript.
[object Object]
Function objects:
stringify(function (){}) ->
[object Function]
Array objects:
stringify([]) ->
[object Array]
RegExp objects
stringify(/x/) ->
[object RegExp]
Date objects
stringify(new Date) ->
[object Date]
Object objects!
stringify({}) ->
[object Object]
4 pillars (concepts of OOP)
encapsulation
abstraction
inheritance
polymorphism
encapsulation does
reduce complexity + increase reusability
abstraction
reduce complexity + isolate impact of change
that hides the internal implementation details
inheritance
eliminate redundant code
polymorphism
refactor switch / case statement
What are the 3 Attributes of Object Data Properties?
- Configurable - tells if you can delete or change
- Enumerable - can be returned in a for/in loop
- Writable - property can be changed
Difference between class and an object?
An object is an instance of a class. Objects hold any information , but classes don’t have any information. Definition of properties and functions can be done at class and can be used by the object.
Class can have sub-classes, and an object doesn’t have sub-objects.
typeof Object //
‘function’
typeof new Object() //
‘object’
The__________ method is a special method for creating and initializing an object created within a class.
constructor
function A() {} var a = new A(); console.log(a.constructor); //
ƒ A() {}
function A() {} var a = new A(); console.log(A.constructor); //
ƒ Function() { [native code] }
Object.prototype.a = 5;
var v = {}; console.log(v.a); //
5
prototype of Object.prototype can be altered
TRUE OR FALSE
TRUE