当前位置:网站首页>JS instantiation method
JS instantiation method
2022-07-28 14:22:00 【Front cut zz】
Constructor Pattern
function obj1(name,age){
this.name=name;
this.age=age;
this.identity=function(){
var li=document.createElement("p");
var txt=document.createTextNode(" Bai Xiaochun ");
li.appendChild(txt);
document.body.appendChild(li);
}
}
var person2=new obj1(' Bai Xiaochun ',123);
person2.identity();
alert(person2.name);
Factory mode
function obj2(){
var lio=new Object(); // Create objects , Object property assignment
lio.name='lio';
lio.attr=' male ';
lio.identity=function(){
var li=document.createElement("p");
var txt=document.createTextNode(" Bai Xiaochun ");
li.appendChild(txt);
document.body.appendChild(li);
};
return lio;
}
var person=obj2();
//alert(person.name);
Archetypal model
function obj3(){
//this.name='lio';
}
obj3.prototype.name='lio';
obj3.prototype.identity= function (name) {
alert(" It's actually "+name);
};
var person3=new obj3();
// Check whether it is in the instance or in the prototype
alert(person3.hasOwnProperty("name"));
alert(person3.hasOwnProperty("rename"));
person3.identity(' Bai Xiaochun ');
Mixed mode
function obj4(age) {
this.age=age;
this.rename='aaaa';
};
obj4.prototype={
constructor:obj4,
name:'lio',
age:123,
identity: function (name) {
alert(name+" Actually, it's Bai Xiaochun ");
}
};
var person4=new obj4(18);
alert(person4.hasOwnProperty("age"));//true
person4.identity('lio');
边栏推荐
猜你喜欢

MVC model: calendar system

解决uniapp微信小程序canvas不能引入字体的问题

Forage QR code -- online QR code generator

Clickhouse architecture and design

Daily question - Scholarship

用友BIP CRM新品发布,赋能大中型企业营销增长

MeterSphere--开源持续测试平台

软件测试工程师的职业规划
![[lvgl events] Application of events on different components (I)](/img/a8/7c24e68f3506bbef3c2e922729471c.png)
[lvgl events] Application of events on different components (I)

QT self-made soft keyboard is the most perfect and simple, just like its own virtual keyboard
随机推荐
协同办公工具:在线白板初起步,在线设计已红海
【LeetCode】1331. 数组序号转换
Four ways to create thread pools
【服务器数据恢复】HP StorageWorks系列服务器RAID5两块盘离线的数据恢复
Super resolution reconstruction based on deep learning
7.27 simulation summary
Niuke multi school link with level editor i- (linear DP)
[ecmascript6] class
手机滚动截屏软件推荐
[utils] fastdfs tool class
如何有效进行回顾会议(上)?
webSocket聊天
HCIP第十天
Redis sentinel mechanism
MeterSphere--开源持续测试平台
[ecmascript6] async and await
AFNetworking速成教程
2022年熔化焊接与热切割考题及在线模拟考试
复制excel行到指定行
Postgresql14 installation and master-slave configuration