|
INTERVIEW QUESTIONS
WEB
JAVA SCRIPT
DETAILS
Question: How to associate functions with objects using JavaScript?
Answer: 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; } 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 7362 users |
Added on |
9/22/2014 |
Views |
70944 |
Rate it! |
|
|
Question:
How to associate functions with objects using JavaScript?
Answer:
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; } 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 |
|
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
|
|
How to delete an array entry using JavaScript?
|
View Answer
|
|
What does the delete operator do?
|
View Answer
|
|
What's the Date object using JavaScript?
|
View Answer
|
|
What's Math Constants and Functions using JavaScript?
|
View Answer
|
|
How to test for bad numbers using JavaScript?
|
View Answer
|
|
How to convert numbers to strings using JavaScript?
|
View Answer
|
|
How to convert a string to a number using JavaScript?
|
View Answer
|
|
How to force a page to go to another page using JavaScript?
|
View Answer
|
|
How to reload the current page?
|
View Answer
|
|
How to set the cursor to wait?
|
View Answer
|
|
How to make elements invisible?
|
View Answer
|
|
How to change style on an element?
|
View Answer
|
|
How to remove the event listener?
|
View Answer
|
|
How to Handle Event Handlers?
|
View Answer
|
|
How to getting values from cookies to set widgets?
|
View Answer
|