JavaScript: Constructor and Object Oriented Programming

Constructor : 

Grammar:

     object.constructor

Example:

Javascript code:

1
function obj1() { 2 this.number = 1; 3 } 4 5 var x1 = new String("Computer"); 6 7 if (x1.constructor == String) 8 document.write("This object is a String."); 9 10 var x2 = new obj1; 11 12 if (x2.constructor == obj1) 13 document.write("This object constructor is obj1.");

The code will output: 

This object is a String.
This object constructor is Obj1.




The influence of Object Oriented Programming:

we can konw what effect Constructor has.

It can show the type that a unkown object and check the type of object we build right or not.

It also the representative in Javascript of Object Oriented Programming.

So,we can say that :

In Javascript,Constructor is the source of Object Oriented Programming
posted @ 2018-11-11 19:42  陈府  阅读(118)  评论(0编辑  收藏  举报