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

Is javascript necessary?

532


What is difference between array.splice() and array.slice() method in javascript?

581


What is the difference between call & apply?

561


How do you implement Ajax using hide() function in JQuery?

1891


How to read elements of an array in JavaScript?

603






What percentage of websites use javascript?

569


What is let keyword in typescript?

643


What is the use of a weakmap object in javascript?

573


What are the ways to define a variable in javascript?

585


What is the === in javascript?

540


20 Functions of Selenium with description and Examples.

2406


Can you give an example showing javascript hoisting?

523


What is an asynchronous programming? Why is it important in javascript?

617


What is camelcase in programming?

515


What do you mean by enumeration?

556