提起组织函数,我们需要从JS的建立工具最先,JS的建立工具有两种方式,一种是工具字面量法(把一个工具的属性和方式逐一枚举出来),工具字面量法有一个显著的不足在于它不适合批量的或者是构建大量的类似或者重复的工具,由于这个限制也就有了另一种建立方式,组织函数。
工具字面量
const person = {
name: 'Eric',
age: 28,
school: '侠课岛',
getName: function() {
return this.name;
},
getAge: function() {
return this.age;
},
};
组织函数
function Person(name, age) {
this.name = name || '';
this.age = age || 0;
school: '侠课岛',
this.getName = function() {
return this.name;
}
this.getAge = function() {
return this.age
}
}
const Eric = new Person('Eric', 26);
什么是组织函数? 组织函数是一个构建工具的函数,在建立工具时就初始化工具,为工具成员变量赋予一个初始值,好比上面的person函数内里若是没有传name和age进去,那么默认的name的初始值就是空,age就是0。它总是与new运算符一起使用在建立工具语句中,组织函数最主要的特点在于它利便建立多个工具的实例。
/* 组织函数 */
function Person() {
console.log('this', this);
}
const A = new Person();
/* 通俗函数 */
function person() {
console.log('this', this);
}
const B = person();
function Student(name){
this.name = name || '';
}
function Teacher(name){
this.name = name || '';
}
const Davy = new Teacher('Davy');
concole.log(Davy);
function Driver(name){
this.name = name;
return {
name: 'xxx';
};
}
const Micheal = new Driver('Micheal');
console.log(Micheal);
var obj = {};
obj.__proto__ = constructorFunction.prototype;
var result = constructorFunction.call(obj);
typeof result === 'object' ? result : obj;
var obj = {};
obj.__proto__ = constructorFunction.prototype;
constructorFunction.call(obj);
return obj;
1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入
2.腾讯云: 提供云服务器、云数据库、云存储、视频与CDN、域名等服务。腾讯云各种产物的最新流动,优惠券领取点击进入
3.广告同盟: 整理了现在主流的广告同盟平台,若是你有流量,可以作为参考选择适合你的平台点击进入
链接: http://www.fly63.com/article/detial/4247