当前位置:网站首页>JS study note 04-05 - modification of constructor and creation using factory method
JS study note 04-05 - modification of constructor and creation using factory method
2022-07-19 08:30:00 【The man fished alone in the cold river snow】
<!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 scope 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);
};
};
// Use instanceof You can check whether an object is an instance of a class
//console.log(per instanceof Person); return true perhaps false
var per = new Person(" Lei Yunteng ", " male ", 19, " Leshan City ");
console.log(per) // The constructor above will create a new method every time , perform n Session creation n The same way , All objects can share the same method function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
// Add a method to the object
this.sayName = fun;
}
// take sayName Methods are defined in the global scope function fun() {
alert("Hello Hello everyone , I am a :" + this.name)
};
// Create a Person object
var person = new Person(" Lei Yunteng ", 19, " male ");
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:" The Monkey King ", // age:38, // gender:" male ", // sayName:function(){
// alert(this.name+this.age+this.gender)
// }
// }
// // Call function
// obj.sayName();
// Use the factory method to create objects // Through this method, objects can be created in large quantities // function createPerson(name,age,gender){
// // Create a new object
// var obj=new Object();
// // Add properties to the 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(" Bones jing ",28," male ");
// console.log(obj2);
function createPeople(name, age, address, gender, job) {
// Create an object
var obj2 = Object();
obj2.name = name;
obj2.age = age;
obj2.address = address;
obj2.gender = gender;
obj2.job = job;
obj2.fun = function () {
alert(" The name is :" + this.name + " Age is :" + this.age + " Address :" + this.address + " Gender :" + this.gender + " Work :" + this.job)
}
// Return the new object to
return obj2;
};
var obj3 = createPeople(" Lei Yunteng ", 19, " Leshan City ", " male ", " Student ");
console.log(obj3) // Constructors are usually capitalized Use this Call without setting the return value 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);
};
};
// Use instanceof You can check whether an object is an instance of a class
//console.log(per instanceof Person); return true perhaps false
var per = new Person(" Lei Yunteng ", " male ", 19, " Leshan City ");
console.log(per)
</script>
</head>
<body>
</body>
</html>
边栏推荐
猜你喜欢

Detailed explanation of ansible automatic operation and maintenance (IV) preparation, use, command execution and example demonstration of playbook in ansible

Database write Optimization: database and table segmentation and related issues

使用arduino开发esp8266和esp32时首选项设置方法

凸面镜面3D玻璃轮廓扫描

STM32CUBEIDE(9)----USART通过DMA收发

Redis 概述安装

力扣1669合並兩個鏈錶筆記

Visual studio 2022 (vs 2022) cannot read memory

Picture browser

OpenCV极坐标转换函数warpPolar的使用
随机推荐
JS学习笔记14-15:JS数组及数组字母量
php存储密码
3D激光SLAM:ALOAM---帧间里程计代码解读
事件循环、宏任务、微任务
半透明双层玻璃侧厚
Stm32subeide (9) -- USART sends and receives via DMA
Address monitoring API: how to trace and monitor uniswap hacker addresses
New redis6 features
JS学习笔记09-12:原型对象以及Foreach+tostring及回收站
Redis 概述安装
Seaport 以及 ERC-4907 能否成为释放NFT流动性的新途径?| Tokenview
Talk about distributed locks
手把手实践一个DAPP,通往Web3.0之路!
No module named ‘yaml‘ 解决办法
Redis common data types - redis list and redis set
How to select MCU?
65、Restful规范
Dependency injection method
leetcode:287. 寻找重复数【快慢指针板子】
Redis介绍