当前位置:网站首页>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');
边栏推荐
- Forage QR code -- online QR code generator
- jenkins
- 【LeetCode】1331. 数组序号转换
- 离散对数问题(DLP) && Diffie-Hellman问题(DHP)
- MiniTest--小程序自动化测试框架
- 文献阅读(245)Roller
- 7.27 simulation summary
- [ecmascript6] proxy and reflection
- bgp实验
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
猜你喜欢

Recommended super easy-to-use mobile screen recording software

RSA用私钥加密数据公钥解密数据(不是签名验证过程)

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

文献阅读(245)Roller

HCIP第十一天

Career planning of Software Test Engineer

As a programmer, how to manage time efficiently?

如何有效进行回顾会议(上)?

IP black and white list

Foundation of deep learning ---- GNN spectral domain and airspace (continuous improvement, update and accumulation)
随机推荐
走进音视频的世界——FLV视频封装格式
RSA用私钥加密数据公钥解密数据(不是签名验证过程)
How to effectively conduct the review meeting (Part 1)?
Understand the principle behind the virtual list, and easily realize the virtual list
Power amplifier and matching network learning
分集技术简略
Detailed explanation of common commands of vim (VIM use tutorial)
Postgresql14 installation and master-slave configuration
webSocket聊天
Three methods to disassemble the rotation array
Clickhouse distributed cluster construction
How did Dongguan Huawei cloud data center become a new model of green data center?
IP black and white list
Three cases of thread blocking.
阿里、京东、抖音:把云推向产业心脏
多所“双一流”大学,保研预报名启动!
Nport serial server configuration website (whether the serial server is from network port to serial port)
Metersphere -- Open Source continuous testing platform
【Utils】CookieUtil
LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树