当前位置:网站首页>js的实例化方式
js的实例化方式
2022-07-28 13:28:00 【前端切图仔Zz】
构造函数模式
function obj1(name,age){
this.name=name;
this.age=age;
this.identity=function(){
var li=document.createElement("p");
var txt=document.createTextNode("白小纯");
li.appendChild(txt);
document.body.appendChild(li);
}
}
var person2=new obj1('白小纯',123);
person2.identity();
alert(person2.name);
工厂模式
function obj2(){
var lio=new Object(); //创建对象,对象属性赋值
lio.name='lio';
lio.attr='男';
lio.identity=function(){
var li=document.createElement("p");
var txt=document.createTextNode("白小纯");
li.appendChild(txt);
document.body.appendChild(li);
};
return lio;
}
var person=obj2();
//alert(person.name);
原型模式
function obj3(){
//this.name='lio';
}
obj3.prototype.name='lio';
obj3.prototype.identity= function (name) {
alert("实际上是"+name);
};
var person3=new obj3();
//检测是在实例中还是在原型中
alert(person3.hasOwnProperty("name"));
alert(person3.hasOwnProperty("rename"));
person3.identity('白小纯');
混合模式
function obj4(age) {
this.age=age;
this.rename='aaaa';
};
obj4.prototype={
constructor:obj4,
name:'lio',
age:123,
identity: function (name) {
alert(name+"实际上是白小纯");
}
};
var person4=new obj4(18);
alert(person4.hasOwnProperty("age"));//true
person4.identity('lio');
边栏推荐
猜你喜欢

Leetcode 0142. circular linked list II

MiniTest--小程序自动化测试框架

QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样

MVC模型:日历系统

Entering the world of audio and video -- flv video packaging format

83. (cesium home) how the cesium example works

Clickhouse distributed cluster construction

深度学习基础----GNN谱域和空域 (不断完善更新积累)

多所“双一流”大学,保研预报名启动!

How to effectively conduct the review meeting (Part 1)?
随机推荐
2022年熔化焊接与热切割考题及在线模拟考试
[lvgl events] Application of events on different components (I)
【Utils】CookieUtil
用友BIP CRM新品发布,赋能大中型企业营销增长
开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
How to write test cases in software testing technology
Several solutions to spanning
如何有效进行回顾会议(上)?
The default storage engine after MySQL 5.5 is InnoDB.
每日一题——奖学金
QQ robot configuration record based on nonebot2
Xcode编写SwiftUI代码时一个编译通过但导致预览(Preview)崩溃的小陷阱
Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence
MeterSphere--开源持续测试平台
Mobile phone scrolling screenshot software recommendation
Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
Solve the problem that uniapp wechat applet canvas cannot introduce fonts
Nport serial server configuration website (whether the serial server is from network port to serial port)
牛客多校-Link with Level Edito I-(线性dp)
7.27 simulation summary