|
INTERVIEW QUESTIONS
WEB
JAVA SCRIPT
DETAILS
Question: How to associate functions with objects using JavaScript?
Answer: Let's now create a custom "toString()" method for our movie object. We can embed the function directly in the object like this.
<script type="text/javascript"> function movie(title, director) { this.title = title; this.director = director; this.toString = function movieToString() { return("title: "+this.title+" director: "+this.director); } } var narnia = new movie("Narni","Andrew Adamson"); document.write(narnia.toString()); </script> This produces title: Narni director: Andrew Adamson
Or we can use a previously defined function and assign it to a variable. Note that the name of the function is not followed by parenthisis, otherwise it would just execute the function and stuff the returned value into the variable.
<script type="text/javascript"> function movieToString() { return("title: "+this.title+" director: "+this.director); } function movie(title, director) { this.title = title; this.director = director; this.toString = movieToString; //assign function to this method pointer } var aliens = new movie("Aliens","Cameron"); document.write(aliens.toString()); </script> This produces title: Aliens director: Cameron
|
|
|
Category |
Java Script Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8413 users |
Added on |
10/29/2014 |
Views |
67638 |
Rate it! |
|
|
Question:
How to associate functions with objects using JavaScript?
Answer:
Let's now create a custom "toString()" method for our movie object. We can embed the function directly in the object like this.
<script type="text/javascript"> function movie(title, director) { this.title = title; this.director = director; this.toString = function movieToString() { return("title: "+this.title+" director: "+this.director); } } var narnia = new movie("Narni","Andrew Adamson"); document.write(narnia.toString()); </script> This produces title: Narni director: Andrew Adamson
Or we can use a previously defined function and assign it to a variable. Note that the name of the function is not followed by parenthisis, otherwise it would just execute the function and stuff the returned value into the variable.
<script type="text/javascript"> function movieToString() { return("title: "+this.title+" director: "+this.director); } function movie(title, director) { this.title = title; this.director = director; this.toString = movieToString; //assign function to this method pointer } var aliens = new movie("Aliens","Cameron"); document.write(aliens.toString()); </script> This produces title: Aliens director: Cameron Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
What does break and continue statements do?
|
View Answer
|
|
How to create a function using function constructor?
|
View Answer
|
|
What's Prototypes for JavaScript?
|
View Answer
|
|
How to add Buttons in JavaScript?
|
View Answer
|
|
Example of using Regular Expressions for syntax checking in JavaScript?
|
View Answer
|
|
How to create arrays in JavaScript?
|
View Answer
|
|
How do you convert numbers between different bases in JavaScript?
|
View Answer
|
|
What are JavaScript Data Types?
|
View Answer
|
|
How do you submit a form using JavaScript?
|
View Answer
|
|
What are decodeURI() and encodeURI() functions in JavaScript?
|
View Answer
|
|
What's Prototypes for JavaScript?
|
View Answer
|
|
How to create a function using function constructor?
|
View Answer
|
|
What does break and continue statements do in JavaScript?
|
View Answer
|
|
What is eval() in JavaScript?
|
View Answer
|
|
How to associate functions with objects using JavaScript?
|
View Answer
|
|
How to create an object using JavaScript?
|
View Answer
|
|
How to shift and unshift using JavaScript?
|
View Answer
|
|
How to make a array as a stack using JavaScript?
|
View Answer
|
|
How to use "join()" to create a string from an array using JavaScript?
|
View Answer
|
|
How to use strings as array indexes using JavaScript?
|
View Answer
|