0% found this document useful (0 votes)
2 views

JavaScript_Assignment_Answers

The document explains key JavaScript concepts including object prototypes and inheritance, property querying and setting, various object creation methods, function invocation styles, treating functions as values, and modularity in ES6 with import and export keywords. Examples are provided for each concept to illustrate their usage. Overall, it serves as a comprehensive guide to fundamental JavaScript features and practices.

Uploaded by

kilaruharika2003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaScript_Assignment_Answers

The document explains key JavaScript concepts including object prototypes and inheritance, property querying and setting, various object creation methods, function invocation styles, treating functions as values, and modularity in ES6 with import and export keywords. Examples are provided for each concept to illustrate their usage. Overall, it serves as a comprehensive guide to fundamental JavaScript features and practices.

Uploaded by

kilaruharika2003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JavaScript Assignment Answers

1. Outline object prototype and prototype inheritance and explain with


example.
In JavaScript, every object has an internal property called [[Prototype]], which is either
another object or null.
Prototype inheritance allows objects to inherit properties and methods from other objects.
This is useful for code reuse and for implementing object-oriented programming.

Example:

// Define a constructor function


function Animal(name) {
this.name = name;
}

// Add a method to the prototype


Animal.prototype.speak = function() {
console.log(this.name + ' makes a noise.');
};

// Create an object using the constructor


const dog = new Animal('Dog');

// Inherit properties from the prototype


dog.speak(); // Dog makes a noise.

2. How properties are queried and set in JavaScript? Explain in detail.


Properties in JavaScript objects can be queried (read) or set using dot notation or bracket
notation.
Dot notation is used for simple property names, while bracket notation is used for dynamic
or non-standard property names.

Example:

const obj = { key: 'value' };

// Querying a property
console.log(obj.key); // value
// Setting a property
obj.newKey = 'newValue';
console.log(obj.newKey); // newValue

// Using bracket notation


console.log(obj['key']); // value

3. What are the various ways of creating objects in JavaScript? Explain with
examples.
JavaScript provides multiple ways to create objects:

1. Using Object Literals

const obj = { key: 'value' };

2. Using Object.create

const proto = { greet: function() { console.log('Hello'); } };


const obj = Object.create(proto);
obj.greet(); // Hello

3. Using Constructor Functions

function Person(name) {
this.name = name;
}
const person = new Person('John');
console.log(person.name); // John

4. Explain various ways of invoking functions in JavaScript with examples.


Functions in JavaScript can be invoked in the following ways:

1. As a Function

function greet() {
console.log('Hello');
}
greet(); // Hello

2. As a Method

const obj = { greet: function() { console.log('Hello'); } };


obj.greet(); // Hello

3. Using Call and Apply

function greet() {
console.log(this.name);
}
const obj = { name: 'Alice' };
greet.call(obj); // Alice

5. Explain Functions as values with code examples.


In JavaScript, functions are treated as first-class citizens, meaning they can be stored in
variables, passed as arguments, and returned from other functions.

Example:

const greet = function(name) {


return `Hello, ${name}`;
};

console.log(greet('John')); // Hello, John

6. How modularity is supported in ES6 with import and export keywords?


ES6 introduced modules, allowing code to be split into reusable pieces using `export` and
`import` keywords.
`export` is used to share code, and `import` is used to bring the code into another file.

Example:

File: utils.js

export function greet(name) {


return `Hello, ${name}`;
}

File: main.js

import { greet } from './utils.js';


console.log(greet('Alice')); // Hello, Alice

You might also like