Docsity

Advanced JavaScript Exam: Questions and Answers, Exams of Advanced Education

Toggle sidebar
bg1
ADVANCED JAVASCRIPT EXAM
QUESTIONS AND ANSWERS
What does it mean that "JavaScript is a prototype-based language?" - Correct Answers
-Object properties and methods can be shared through generalized objects-blueprints-
that have that ability to be cloned and extended.
1. Create an object that is a prototype.
2. create a new instance = prototype name.
3. You can add properties and methods to the prototype part of JavaScript.
An object in JavaScript is a data type that is composed of a collection of names or keys
and values, represented in name:value pairs. - Correct Answers -True
The name:value pairs in an object can consist of properties that may contain any data
type โ€” including strings, numbers, and Booleans โ€” as well as methods, which are
functions contained within an object. - Correct Answers -True
Other languages use classes as blueprints rather than constructor functions. - Correct
Answers -True
What are 2 ways to create an object? - Correct Answers -The object literal, which uses
curly brackets: {}
The object constructor, which uses the new keyword.
An object is a data type. - Correct Answers -True
Any data type can be store in a variable. - Correct Answers -True
What's an example of an object literal? - Correct Answers -// Initialize object literal with
curly brackets
const objectLiteral = {};
How do you create an object with a constructor function? - Correct Answers -// Initialize
object constructor with new Object
const objectConstructor = new Object();
Example of an object - Correct Answers -// Initialize gimli object
pf3
pf4
pf5

Partial preview of the text

Download Advanced JavaScript Exam: Questions and Answers and more Exams Advanced Education in PDF only on Docsity!

ADVANCED JAVASCRIPT EXAM

QUESTIONS AND ANSWERS

What does it mean that "JavaScript is a prototype-based language?" - Correct Answers -Object properties and methods can be shared through generalized objects-blueprints- that have that ability to be cloned and extended.

  1. Create an object that is a prototype.
  2. create a new instance = prototype name.
  3. You can add properties and methods to the prototype part of JavaScript. An object in JavaScript is a data type that is composed of a collection of names or keys and values, represented in name:value pairs. - Correct Answers -True The name:value pairs in an object can consist of properties that may contain any data type โ€” including strings, numbers, and Booleans โ€” as well as methods, which are functions contained within an object. - Correct Answers -True Other languages use classes as blueprints rather than constructor functions. - Correct Answers -True What are 2 ways to create an object? - Correct Answers -The object literal, which uses curly brackets: {} The object constructor, which uses the new keyword. An object is a data type. - Correct Answers -True Any data type can be store in a variable. - Correct Answers -True What's an example of an object literal? - Correct Answers -// Initialize object literal with curly brackets const objectLiteral = {}; How do you create an object with a constructor function? - Correct Answers -// Initialize object constructor with new Object const objectConstructor = new Object(); Example of an object - Correct Answers -// Initialize gimli object

const gimli = { name: "Gimli", race: "dwarf", weapon: "axe", greet: function() { return Hi, my name is ${this.name}!; }, }; How many properties does the gimli object have? - Correct Answers - name:value pair, also known as key:value pair - Correct Answers -True What are two ways to access an object's properties? - Correct Answers -Dot notation:. Bracket notation: [] How would I retrieve the weapon property in the gimli object mentioned earlier? - Correct Answers -// Retrieve the value of the weapon property gimli.weapon; Outputs "axe" How would I use [ ] to access the weapon property of the gimili object? - Correct Answers -// Retrieve the value of the weapon property gimli["weapon"]; Outputs: "axe" n order to retrieve an object method, you would call it much in the same way you would call a regular function, just attached to the object variable. gimli.greet(); Output "Hi, my name is Gimli!" - Correct Answers -true What are 2 ways to add a new property to an object? - Correct Answers -// Add new age property to gimli gimli.age = 139; // Add new age property to gimli gimli["age"] = 139;

JavaScript has a built-in type of for loop that is specifically meant for iterating over the properties of an object. This is known as the _________________________ Here is a simplified version of our main object example, gimli. - Correct Answers -for...in loop. const gimli = { name: "Gimli", race: "dwarf", weapon: "battle axe", }; We can use for...in to traverse through all the properties of gimli and print them to the console. Using bracket notation, we can retrieve the property value as a variable, in this case key. // Iterate through properties of gimli for (let key in gimli) { console.log(gimli[key]); } Output Gimli dwarf battle axe - Correct Answers - We can also retrieve the property name itself using just the first variable in the for...in loop. We have used a string method to convert the key values to upper case. // Get keys and values of gimli properties for (let key in gimli) { console.log(key.toUpperCase() + ':', gimli[key]); } Output NAME: Gimli RACE: dwarf WEAPON: battle axe - Correct Answers -true Another useful enumeration method is the Object.keys() method, which will return an array of the object's keys. // Initialize method on gimli object to return property keys Object.keys(gimli); Output ["name", "race", "weapon"] - Correct Answers -true

In addition to objects that are predefined in the browser, you can define your own objects. - Correct Answers -True All objects in JavaScript inherit from at least one other object. The object being inherited from is known as the prototype, and the inherited properties can be found in the prototype object of the constructor. - Correct Answers -True How can prototypes be used to extend objects? - Correct Answers - Every object in JavaScript has an internal property called [[Prototype]]. We can demonstrate this by creating a new, empty object. let x = {}; - Correct Answers -True The double square brackets that enclose [[Prototype]] signify that it is an internal property, and cannot be accessed directly in code. What's another way to create an object? - Correct Answers -with the object constructor: let x = new Object(). To find the [[Prototype]] of this newly created object, we will use the getPrototypeOf() method. Object.getPrototypeOf(x); The output will consist of several built-in properties and methods. Output {constructor: ฦ’, defineGetter: ฦ’, defineSetter: ฦ’, ...} - Correct Answers - Object is the "king" or "top level" contructor function of the JavaScript language. - Correct Answers -True It is important that every object in JavaScript has a [[Prototype]] as it creates a way for any two or more objects to be linked. - Correct Answers - Define the word prototype. - Correct Answers -a first, typical or preliminary model of something, Prototype chaining is used to build new types of objects based on existing ones - Correct Answers -True dding properties and methods to the prototype property will automatically add the method or property to all objects created by the constructor function. - Correct Answers -True

Here's another way to write the same thing, but we are separating the method outside of the constructor function: function Pet(name, species, hello) { this.name = name; this.species = species; this.hello = hello; } Pet.prototype.sayHello = function() { alert(this.hello); } var rufus = new Pet("Rufus", "cat", "miaow"); rufus.sayHello(); - Correct Answers - When we created the Pet function, its prototype property was automatically set to an empty object. Every function is set up this way when it's created because there's no way for javascript to know whether any particular function will be used as a constructor function or not. We wanted all our pets to have a sayHello method, so we added a sayHello() method to the Pet's prototype property. The prototype property contains a regular javascript object so this is just the same as assigning a new method or property to any other object. In this case we have created a new anonymous function and assigned it to a property called sayHello. When we created the rufus object, it automatically maintained an invisible link back to the Pet function that created it. Then when we tried to access the sayHello() method, javascript first looked for a sayHello() method set directly on the rufus object. When it couldn't find one, it then l - Correct Answers -