How to get the total number of objects created in javascript for a class? -
lets have following code snippet:
function airplane(id) { this.id = id; } var air1 = new airplane(234); var air2 = new airplane(235); var air3 = new airplane(236);
is there way total number of airplane objects created airplane.getcreatedlength()
?
you keep track of in property attached constructor:
function airplane(id) { this.id = id; airplane.instances += 1; } airplane.instances = 0; var air1 = new airplane(234); var air2 = new airplane(235); var air3 = new airplane(236); console.log(airplane.instances) // 3
Comments
Post a Comment