Advanced JavaScript

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How would I retrieve the weapon property in the gimli object mentioned earlier?

// Retrieve the value of the weapon property gimli.weapon; Outputs "axe"

How would I use [ ] to access the weapon property of the gimili object?

// Retrieve the value of the weapon property gimli["weapon"]; Outputs: "axe"

Prototypes are implemented in javascript using ________________

the prototype property of constructor functions.

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"]

true

In other languages, a class is a template for creating an object and an object is a specific instance of a class. Once the object is created you cannot make it into a different type of object by adding different properties.

true

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

true

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!"

true

What's another way to create an object?

with the object constructor: let x = new Object().

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.

for...in loop.

How can we access the value 139 from gimli.age?

, with either the dot notation or the bracket notation. gimli.age; gimli[age];

What are 2 ways to add a new property to an object?

// Add new age property to gimli gimli.age = 139; // Add new age property to gimli gimli["age"] = 139;

Example of an object

// Initialize gimli object const gimli = { name: "Gimli", race: "dwarf", weapon: "axe", greet: function() { return `Hi, my name is ${this.name}!`; }, };

How do you create an object with a constructor function?

// Initialize object constructor with new Object const objectConstructor = new Object();

What's an example of an object literal?

// Initialize object literal with curly brackets const objectLiteral = {};

How many properties does the gimli object have?

3

What are two ways to access an object's properties?

Dot notation: . Bracket notation: []

What does the prototype property contain?

It contains the things that should be the same among all objects created by the constructor. Individual objects can override and change the things they inherit from the constructor's prototype but they will all start out the same.

What does it mean that "JavaScript is a prototype-based language?"

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.

A method can also be added to the object by using the same process. // Add new fight method to gimli gimli.fight = function() { return `Gimli attacks with an ${this.weapon}.`; }

Once we have created this new object method, we can call it as we did above. gimli.fight(); Output "Gimli attacks with an axe."

This approach is called prototype-based inheritance because an existing object is used as a template to build other objects. The existing object will be used as a starting point to build the new object but the new object does not have to be exactly the same.

True

dding properties and methods to the prototype property will automatically add the method or property to all objects created by the constructor function.

True

name:value pair, also known as key:value pair

True

Removing Object Properties In order to remove a property from an object, you will utilize the delete keyword. delete is an operator that removes a property from an object. In the below example, we will remove the weapon property from gimli using delete. // Remove weapon from gimli delete gimli.weapon; Output true

The delete operation evaluates as true if the property was successfully removed, or if it was used on a property that doesn't exist. We can test the output of gimli to see if it succeeded. gimli; Output {name: "Gimli", race: "dwarf", age: 139, greet: ƒ, fight: ƒ} In the above output, the weapon name and its associated value are no longer available, showing that we have successfully deleted the property.

What are 2 ways to create an object?

The object literal, which uses curly brackets: {} The object constructor, which uses the new keyword.

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.

True

Although a constructor function may look like a class (probably so people from more traditional class based languages would be able to use it), it's really just a function that knows how to add the right properties and methods to create a particular type of object.

True

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.

True

An object is a data type.

True

Any data type can be store in a variable.

True

Any property or method that's added to the constructor's prototype automatically becomes part of every object created by that function.

True

Function.prototype property represents the Function prototype object.

True

In addition to objects that are predefined in the browser, you can define your own objects.

True

Object is the "king" or "top level" contructor function of the JavaScript language.

True

Other languages use classes as blueprints rather than constructor functions.

True

Prototype chaining is used to build new types of objects based on existing ones

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.

True

The prototype property is basically a template for the objects created by the constructor.

True

Every object in JavaScript has an internal property called [[Prototype]]. We can demonstrate this by creating a new, empty object. let x = {};

True The double square brackets that enclose [[Prototype]] signify that it is an internal property, and cannot be accessed directly in code.

Define the word prototype.

a first, typical or preliminary model of something,

How do we update weapon from axe to battle axe?

gimli.weapon = "battle axe";


Set pelajaran terkait

Chapter 5 - Separate and Together: Life in Groups

View Set

Chapter 8: Project Quality Management

View Set