当前位置:网站首页>JS学习笔记04-05——构造函数的修改以及使用工厂方法创建
JS学习笔记04-05——构造函数的修改以及使用工厂方法创建
2022-07-17 07:28:00 【那人独钓寒江雪.】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> //函数作用域 function Person(name, age, gender, address) {
this.name = name;
this.age = age;
this.gender = gender this.address = address this.fun = function () {
alert(this.name + this.gender + this.age + this.address);
};
};
//使用instanceof可以检查一个对象是否是一个类的实例
//console.log(per instanceof Person); 返回true或者false
var per = new Person("雷云腾", "男", 19, "乐山市");
console.log(per) //上面的构造函数每一次都会创建一个新的方法,执行n次会创建n个一模一样的方法,完全可以使所有的对象共享同一个方法 function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
//向对象中添加一个方法
this.sayName = fun;
}
//将sayName方法在全局作用域中定义 function fun() {
alert("Hello大家好,我是:" + this.name)
};
//创建一个Person对象
var person = new Person("雷云腾", 19, "男");
console.log(person);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> // var obj={
// name:"孙悟空", // age:38, // gender:"男", // sayName:function(){
// alert(this.name+this.age+this.gender)
// }
// }
// //调用函数
// obj.sayName();
//使用工厂方法创建对象 //通过该方法可以大批量的创建对象 // function createPerson(name,age,gender){
// //创建一个新的对象
// var obj=new Object();
// //向对象中添加属性
// obj.name=name;
// obj.age=age;
// obj.gender=gender;
// obj.sayName=function(){
// alert(this.name+this.age+this.gender);
// };
// return obj;
// }
// var obj2=createPerson("白骨精",28,"男");
// console.log(obj2);
function createPeople(name, age, address, gender, job) {
//创建一个对象
var obj2 = Object();
obj2.name = name;
obj2.age = age;
obj2.address = address;
obj2.gender = gender;
obj2.job = job;
obj2.fun = function () {
alert("名字为:" + this.name + "年龄为:" + this.age + "地址:" + this.address + "性别:" + this.gender + "工作:" + this.job)
}
//将新的对象返回
return obj2;
};
var obj3 = createPeople("雷云腾", 19, "乐山市", "男", "学生");
console.log(obj3) //构造函数首字母通常大写 使用this调用不用设置返回值 function Person(name, age, gender, address) {
this.name = name;
this.age = age;
this.gender = gender this.address = address this.fun = function () {
alert(this.name + this.gender + this.age + this.address);
};
};
//使用instanceof可以检查一个对象是否是一个类的实例
//console.log(per instanceof Person); 返回true或者false
var per = new Person("雷云腾", "男", 19, "乐山市");
console.log(per)
</script>
</head>
<body>
</body>
</html>
边栏推荐
猜你喜欢

深度学习之 7 深度前馈网络

Eureka基础知识

数组习题三

畅玩JVM——关于GC垃圾回收必须要掌握的知识

Deep learning 7 deep feedforward network

Redis常用数据类型——哈希(Hash)和有序集合 Zset(sorted set)
![[C# 变量常量关键字]- C# 中的变量常量以及关键字](/img/9b/433f9110d9c7599d8beac8288ea409.png)
[C# 变量常量关键字]- C# 中的变量常量以及关键字

Dark horse programmer - software testing -16 stage 3 - function testing -175-198, URL composition introduction, request content and composition description line function test and database, URL composi

Redis6新数据类型——HyperLogLog

【flask入门系列】请求钩子与上下文
随机推荐
剑指 Offer 42. 连续子数组的最大和-动态规划法
Redis常用数据类型——Redis列表(List)和Redis 集合(Set)
OI回忆录
TextView文字上下移动
Use of OpenCV polar transformation function warppolar
MySQL 2502 2503 error
Textview text up and down
No module named ‘yaml‘ 解决办法
在VSCode中设置settings.json
5.1 vulnérabilités et précautions en matière de sécurité
Yolov5 label and establish your own data set
[characteristic Engineering]
Is it necessary to buy pension insurance? What are the pension products suitable for the elderly?
[C# Console]-C# 控制台类
Why does the Fed cut interest rates benefit the digital money market in the long run? 2020-03-05
Real case: how to check the soaring usage of CPU after the system goes online?
WVPPRO-ZLM-GB21818-摄像头
Array exercise 3
Demo collection injection
Oi memoirs