JavaScript Objects and Prototypes

Ace your homework & exams now with Quizwiz!

delete dog.age;

var dog = { name: 'fido', breed: 'dachshund', age: 12 } how would you remove the age property from the dog object?

'use strict' Object.defineProperty(dog, 'name', {writeable: false})

var dog = { name: 'fido', breed: 'dachshund'} If you want to make the name property of dog not changeable what do you do?

student.fullName = 'John Doe';

var student = {}; Object.defineProperty(student, 'fullName', { set: function(value) { this.name.fullName = value; } } ); How do you set the full name of student to 'John Doe''?

assigns this to a blank object. otherwise its the window object

what does the new keyword do?

object instance

an object's prototype is the ___________ from which the object is inherited

object instance prototype

A function's prototype is the ____________ that becomes the ____________ for all objects using this function as a constructor

student.fullName = 'John Doe';

How would you set the new fullName property using the set syntax... var student = {}; Object.defineProperty(student, 'student', { set: function(value) { this.name.fullName = value; } } );

simply do it dog.disposition = 'calm'; //will add a disposition property to dog object

If you want to add a property to an object after the object has been created, you do this...

Person.prototype.fullName = function() { return this.first + ' ' + this.last }

Consider this code: function Person(name, family) { this.first= name; this.last= last; } write a function that will concatenate first, last for all instances of the Person object

NO. unlike a function contained in the Dog object, a prototype function doesn't have access

Consider this code: var Dog = function (name, breed) { var _foo = 'some internal value' this.name = name; this.breed = breed; } //then we add a prototype.. Dog.prototype.SayHello = function (name) { var _name = name; console.log('hello: ', _name) }; does the SayHello function have access to the _foo variable in the Dog object?

b. fluffy.__proto__

Given the following code: function Cat(name, color) { this.name = name; this.color = color; } var fluffy = new Cat('Fluffy', 'White') What is the correct way to access fluffy's prototype in most browsers? a. Cat.__proto__ b. fluffy.__proto__ c. Cat.prototype d. fluffy.prototype

3

Given the following code: function Cat(name, color) { this.name = name this.color = color } Cat.prototype.age = 3 var fluffy = new Cat('Fluffy', 'White') Cat.prototype = {age: 5} What does fluffy.age return?

'use strict'

How do you prevent javascript from running old deprecated code and/or not enforcing property rules on objects? Hint: its a declaration on the first line.

Add a prototype to Dog Dog.prototype.Speak = function() { console.log('woof'); }

Look at the following code: var Dog = function (name, breed) { this.name = name; this.breed = breed; } How would you add a speak function to all instances of Dog - but leaving the Dog object intact?

b. object literals var dog = { name: 'boone', breed: 'hound' };

What is the simplest way to create objects? a. Constructor functions b. object literals c. Object.create d. ES 6 Class creation syntax

window object

What is the value of this before you assign it to anything

super()

When inheriting from another class using the class syntax, how would you call the parent class' constructor function? a. super() b. this.protoype() c. this.protoype.constructor() d. base()

a They are just like any other function, but they use the 'this' keyword to assign values to new objects.

Which of the following best describes constructor functions? a They are just like any other function, but they use the 'this' keyword to assign values to new objects. b They are a special type of function and must be prefixed with the 'const' keyword. c They are just like any other function, except that they are used only in classes. d They are a special type of function and must be prefixed with the 'constructor' keyword.

b. It is used to prevent the configurable and enumerable properties from being changed.

Which of the following is true about the configurable attribute for JavaScript properties? a. It is used to prevent the writable, enumerable, and configurable attributes from being changed. b. It is used to prevent the configurable and enumerable properties from being changed. c. It is used to prevent the value of the property from being changed. d. It is used to prevent the writable and enumerable attributes from being changed.

b. It determines whether a property shows up in a for...in loop.

Which of the following is true about the enumerable attribute for properties? a It is used to create enum data types. b It determines whether a property shows up in a for...in loop. c It is used to denote properties that contain lists such as arrays. d It is used to create counter properties.

cat['First Name']

Which of the following is valid syntax for accessing the 'First Name' property on the 'cat' object? cat('First Name') Object.getPropertyValue(cat, 'First Name') cat.firstName cat['First Name']

writeable

Which property attribute would you use to prevent a property's value from being changed?

Dog.prototype = { age: 3 }

You can define a prototype for the dog object like so: Dog.prototype.age = 3; do the same thing using object syntax


Related study sets

OPMA 3306 Chapter 3 (Forecasting)

View Set