- 没有设置返回值
function fruit(type) {
this.type = type;
console.log("this====", this);
}
let fruit1 = new fruit("apple"); // fruit {type: "apple"}
let fruit2 = fruit("apple"); //window
console.log('fruit1', fruit1) //fruit {type: "apple"}
console.log('fruit2', fruit2) //undefined
- 设置返回值 类型为String Boolean Number
function fruit(type) {
this.type = type;
console.log("this====", this);
return this.type;
}
let fruit1 = new fruit("banana"); // fruit {type: "banana"}
let fruit2 = fruit("banana"); //window
console.log('fruit1', fruit1) //fruit {type: "banana"}
console.log('fruit2', fruit2) //"banana"
- 设置返回值 类型为Object Array Function
function fruit(type) {
this.type = type;
console.log("this====", this);
return {
type: this.type
};
}
let fruit1 = new fruit("pear"); // fruit {type: "pear"}
let fruit2 = fruit("pear"); //window
console.log('fruit1', fruit1) //{type: "pear"}
console.log('fruit2', fruit2) //{type: "pear"}
总结: 一个是函数调用 一个是对象实例创建