JS高级:面向对象的构造函数
- 创建对象的方式#
- 1.1 字面量的方式创建对象#
- 1.2 内置构造函数的方式#
- 1.3 简单工厂的方式#
- 1.4 自定义构造函数方式#
- 1.5 矩形案例#
1 创建对象的方式#
1.1 字面量的方式创建对象#
var p1 = {
name: '张三',
run: function () {
console.log(this.name + '跑');
}
};
var p2 = {
name: '李四',
run: function () {
console.log(this.name + '跑');
}
};
console.log(p1.name);
p1.run();
console.log(p2.name);
p2.run();
1.2 内置构造函数的方式#
var p1 = new Object();
p1.name = '张三';
p1.run = function () {
console.log(this.name + '跑');
}
问题:使用内置构造函数的方式和字面量的方式来创建对象差不多,都存在以下问题:
- 创建的对象无法复用,复用性差
- 如果需要创建多个同类型的对象,如(书籍)则需要写大量重复的代码,代码的冗余度高
1.3 简单工厂的方式#
function createPerson(name){
var obj=new Object();//1.原料
//2.加工
obj.name=name;
obj.showName=function(){
alert(this.name);
}
return obj;//3.出厂
}
工厂里面有一些产品的模板, 只需要, 给工厂提供原材料; 工厂按照固定的加工方式, 就可以返回给外界同一类型的产品
问题:无法判定类型
1.4 自定义构造函数方式#
1.4.1 普通#
function Dog(name, age, dogFriends) {
// 属性
this.name = name;
this.age = age;
this.dogFriends = dogFriends;
// 行为
this.eat = function (someThing) {
console.log(this.name + '吃' + someThing);
};
this.run = function (someWhere) {
console.log(this.name + '跑' + someWhere);
}
}
// 产生对象
var smallDog = new Dog('小花', 1);
console.log(smallDog.name, smallDog.age);
- 函数的首字母大写(用于区别构造函数和普通函数)
- 创建对象的过程是由new关键字实现
- 在构造函数内部会自动的创建新对象,并赋值给this指针
- 自动返回创建出来的对象
1.4.2 参数优化#
function Dog(options) {
// 属性
this.name = options.name;
this.age = options.age;
this.dogFriends = options.dogFriends;
// 方法
this.eat = function (someThing) {
console.log(this.name + '吃' + someThing);
};
this.run = function (someWhere) {
console.log(this.name + '跑' + someWhere);
}
}
1.4.3 改进#
function Dog(option) {
// 属性
this.name = option.name;
this.age = option.age;
this.dogFriends = option.dogFriends;
}
Dog.prototype.eat = function (someThing) {
console.log(this.name + '吃' + someThing);
};
Dog.prototype.run = function (someWhere) {
console.log(this.name + '跑' + someWhere);
};
1.4.4 最后版#
function Dog(option) {
this._init(option)
}
Dog.prototype = {
_init: function(option){
// 属性
this.name = option.name;
this.age = option.age;
this.dogFriends = option.dogFriends;
},
eat: function (someThing) {
console.log(this.name + '吃' + someThing);
},
run: function (someWhere) {
console.log(this.name + '跑' + someWhere);
}
};
1.5 矩形案例#
function Rect(options) {
this._init(options)
}
Rect.prototype = {
_init: function (options) {
options = options || {};
// 放在哪里?
this.parentId = options.parentId;
// 位置
this.left = options.left || 0;
this.top = options.top || 0;
// 自身属性
this.width = options.width || 0;
this.height = options.height || 0;
this.bgColor = options.bgColor || '#000';
this.borderRadius = options.borderRadius || 0;
this.border = options.border;
},
render: function () {
// 1. 获取父标签
var parentElement = document.getElementById(this.parentId);
// 2. 创建标签
var childElement = document.createElement('div');
parentElement.appendChild(childElement);
childElement.style.position = 'absolute';
childElement.style.left = this.left + 'px';
childElement.style.top = this.top + 'px';
childElement.style.width = this.width + 'px';
childElement.style.height = this.height + 'px';
childElement.style.backgroundColor = this.bgColor;
childElement.style.border = this.border;
childElement.style.borderRadius = this.borderRadius + 'px';
}
};
// 实例化矩形对象
var rect = new Rect({
parentId:'main',
left:100,
top:200,
width:500,
height:300,
bgColor:'yellow',
border:'10px solid #000',
borderRadius:20
});
rect.render();
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/p/11614480.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具