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

What is javascript in simple terms?

481


How do I get javascript?

512


What does trim stand for?

483


How do I activate javascript on my phone?

500


What is the use of spread operator?

502






Will javascript be replaced?

497


What is a closure and how do you use it?

515


why we need java instead of c

1195


What are the difference between undefined and not defined in javascript?

520


Can I declare a variable as CONSTANT in JavaScript?

532


How to find radio button selection when a form is submitted?

453


What is the use of history object?

563


How do you target a specific frame from a hyperlink in javascript?

480


Which built-in method returns the calling string value converted to lower case?

723


How to remove duplicate values from a javascript array?

511