转 在编写js代码时,我们有时会需要使用函数来模拟java中的类,并用它来产生对象,在定义了一个构造函数之后我们需要使用new操作符来调用调用函数才能得到我们想要的对象.例如: function Consructor(name){ this.name = name } var person1 = Constrcutor("张三");//undefined var person2 = new Constructor("张三");//得到一个对象{name:"…
1.创建一个空对象,并且 this 变量引用该对象,同时还继承了该函数的原型. 2.属性和方法被加入到 this 引用的对象中. 3.新创建的对象由 this 所引用,并且最后隐式的返回 this . var Person = function(name){ //var this = {}; this.name = name; this.say = function(){ return "I am " + this.name; }; //return this; } 简单理解就…