How do you create a new object in JavaScript?

Answer Posted / sri

The following function can be used as demonstrated to create
an object of class myobject:

function myobject() {
this.containedValue = 0;
this.othercontainedValue = 0;
this.anothercontainedValue = 0;
}

var mything = new myobject();
And there you go, mything is now an instance of class
myobject. It will have the following properties, all of
which will be 0:

* mything.containedValue
* mything.othercontainedValue
* mything.anothercontainedValue

You could also now write
myobject.prototype.newContainedValue = someValue; and all
instances of class myobject will have the property
newContainedValue with value someValue.

Is This Answer Correct ?    7 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do I link an external javascript file to html?

645


Why javascript is dangerous?

699


Is not a function error in javascript?

734


What are the uses of javascript in web page designing?

638


What is prototype in javascript and how do you use it?

660


What is class example?

689


How to count the number of element in an array using javascript?

747


Can you assign a anonymous function to a variable?

711


What is the difference between java & javascript?

702


why we need java instead of c

1373


What will be the output of the following statements?

696


Explain what is pop()method in JavaScript?

872


What will happen if an infinite while loop is run in Javascript?

685


How to getting values from cookies to set widgets?

657


What does js stand for?

672