Objects Flashcards

1
Q

How to duplicate a json Safe object

A

var newObj = JSON.parse( JSON.stringify( someObj ) );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to do shallow copy in JS

A

var newObj = Object.assign( {}, myObject );

newObj.a; // 2
newObj.b === anotherObject; // true
newObj.c === anotherArray; // true
newObj.d === anotherFunction; // true)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to explicitly define a property on a object

A

var myObject = {};

Object.defineProperty( myObject, "a", {
	value: 2,
	writable: true,
	configurable: true,
	enumerable: true
} );

myObject.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to prevent new properties being added to a object

A
var myObject = {
	a: 2
};

Object.preventExtensions( myObject );

myObject.b = 3;
myObject.b;

Or
Object.seal(myObject)which internally calls Object.preventExtensions(myObject)

Or Object.freeze(myObject) which internally calls
Object.seal(..)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is [[Get]]

A
var myObj = {a:2}
myObj.a; // performs a [[Get]] operation on teh objec t to get the value of a
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to check if a property exists in object

A
var myObject = {
	a: 2
};
//Checks teh prototype as well
("a" in myObject);				// true
("b" in myObject);				// false
//checks only the object, ignores the prototype chain
myObject.hasOwnProperty( "a" );	// true
myObject.hasOwnProperty( "b" );	// false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is teh output
var myObject = {a:10}
console.log(myObject.b)

A

undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain Polymorphism in JS

A
Polymorphism in JS is rather Explicit and not Relative like Other languages.
Basically all properties of base class which arent in the derived are copied over by a some utility

JavaScript functions can’t really be duplicated (in a standard, reliable way), so what you end up with instead is a duplicated reference to the same shared function object (functions are objects; see Chapter 3). If you modified one of the shared function objects (like ignition()) by adding properties on top of it, for instance, both Vehicle and Car would be “affected” via the shared reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is [Prototype]

A

Objects in JavaScript have an internal property, denoted in the specification as [[Prototype]], which is simply a reference to another object. Almost all objects are given a non-null value for this property, at the time of their creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to check if an object has property built in itself

A

object.hasOwnproperty() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to prototypically link two objects?

A
var obj1 = {a:2}
var obj2 = Object.create(obj1)
console.log(obj2.a)//2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
what is the output
var obj1 = {a:2}
var obj2 = Object.Create(obj1)
obj2.a=10
console.log(obj1.a)
console.log()obj2.a)
A

2

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what are the rules around setting a property on a prototypically linked object

A

If a normal data accessor (see Chapter 3) property named foo is found anywhere higher on the [[Prototype]] chain, and it’s not marked as read-only (writable:false) then a new property called foo is added directly to myObject, resulting in a shadowed property.
If a foo is found higher on the [[Prototype]] chain, but it’s marked as read-only (writable:false), then both the setting of that existing property as well as the creation of the shadowed property on myObject are disallowed. If the code is running in strict mode, an error will be thrown. Otherwise, the setting of the property value will silently be ignored. Either way, no shadowing occurs.
If a foo is found higher on the [[Prototype]] chain and it’s a setter (see Chapter 3), then the setter will always be called. No foo will be added to (aka, shadowed on) myObject, nor will the foo setter be redefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the output?

function Foo(){
console.log("Hello")
}

var a = new Foo();

console. log(a.Constructor)
console. log(a.Constructor === Foo)

A

Foo
Constructor.

The constructor property gets sets on Foo’s prototype when the new keyword is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is the output?

function Foo() { /* .. */ }

Foo.prototype = { /* .. */ }; // create a new prototype object

var a1 = new Foo();

a1. constructor === Foo;
a1. constructor === Object;

A

false
true

fi you create a new object, and replace a function’s default .prototype object reference, the new object will not by default magically get a .constructor on it.

What’s happening? a1 has no .constructor property, so it delegates up the [[Prototype]] chain to Foo.prototype. But that object doesn’t have a .constructor either (like the default Foo.prototype object would have had!), so it keeps delegating, this time up to Object.prototype, the top of the delegation chain. That object indeed has a .constructor on it, which points to the built-in Object(..) function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Foo.prototype.isPrototypeOf( a );

what does this do?

A

The question isPrototypeOf(..) answers is: in the entire [[Prototype]] chain of a, does Foo.prototype ever appear?

17
Q

var a ={}
what is?
a.__proto__

A

Most browsers (not all!) have also long supported a non-standard alternate way of accessing the internal [[Prototype]]:. __proto__ points to the internal prototype object

18
Q

how do classes work in JS

A
Though there is class keyword and inheritance is possible, what actually happens is behaviour delegation.
When a class derives from another, it's actually the _prototype_ property which delegates the work to the base class.
19
Q
what is the output?
var a = {a:10}
var b = Object.Create(a)
b.b=10
a = Object.Create(b)
A

Circular references are disallowed in Javascript

20
Q

what does this do?

Button.prototype = Object.create( Widget.prototype );

A

// make Button “inherit” from Widget