js对象基础写法练习
2024-08-31 16:41:01
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>工厂模式</title> </head> <body> <script> function createPerson(name,age){ var obj = new Object (); obj.uname = name; obj.age =age; obj.showPerson= function(){ console.log(this.age); console.log(this.uname); } return obj; } var man = new createPerson('ajia',23); man.showPerson() </script> </body> </html>
<script> function createPerson(name,age){ this.uname = name; this.age = age; this.showPerson = function(){ console.log(this.uname); console.log(this.age) } } var obj = new createPerson('zjs',23); obj.showPerson(); </script>
<meta charset="utf-8"> <script> function createPeson(name,age){ this.name = name; this.age = age; this.showMf = function(){ this.name= 'aaa---'; console.log(this.name) } } createPeson.prototype.showPerson = function(){ console.log(this.name); console.log(this.age); } var obj = new createPeson('ajia',23); obj.showPerson(); obj.showMf(); </script>
<meta charset="utf-8"> <script> function createPerson(name,age){ this.name = name; this.age = age; //动态创建一个方法 if(typeof createPerson.doWork == 'undefined'){ createPerson.prototype.doWork = function(){ console.log(this.name+'---'+this.age+'---'+'在学习'); } createPerson.doWork = true; } } var person = new createPerson('zjs',23); person.doWork(); console.log(createPerson.doWork) var person1 = new createPerson('ajia',33); person1.doWork(); console.log(createPerson.doWork) </script>
<meta charset="utf8"> <script> //继承1 function Parent(name){ this.name=name; this.sayParent=function(){ console.log("Parent:"+this.name); } } function Child(name,age){ this.tempMethod=Parent; this.tempMethod(name); this.age=age; this.sayChild=function(){ console.log("Child:"+this.name+"age:"+this.age); } } var c = new Child('zjs',23); c.sayChild(); c.sayParent() </script>
<meta charset="utf-8"> <script> // 继承2 function Person(name,age,love){ this.name=name; this.age=age; this.love=love; this.say=function say(){ console.log("姓名:"+name); } } //这里是call方法 function student(name,age){ Person.call(this,name,age); } // 这里是apply方法 function teach(name,love){ // Person.apply(this,[name,love]); Person.apply(this,arguments); } //call与aplly的异同: //1,第一个参数this都一样,指当前对象 //2,第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以) var t = new teach('mayun','baseketball'); t.say() // var p = new Person('zjs',23,'nba'); // var s = new student('ajia',28); // s.say(); </script>
<meta charset="utf-8"> <script> //继承3 function Person(name,age){ this.name=name; this.age=age; } Person.prototype.sayHello=function(){ console.log("使用原型得到Name:"+this.name); } function Student(){} Student.prototype=new Person("洪如彤",21); var stu=new Student(); Student.prototype.grade=5; Student.prototype.intr=function(){ console.log(this.grade); } stu.sayHello();//输出:使用原型得到Name:洪如彤 stu.intr();//输出:5 </script>
最新文章
- PHP的学习--使用phar打包
- android开发 兵器
- ubuntu14.04 python自带版本升级
- MVVM架构~knockoutjs系列之扩展ajax验证~验证输入数据是否与后台数据相等
- @PathVariable注解
- 剑指offer系列26--正则表达式匹配
- 【BZOJ】【2938】【POI2000】病毒
- 使用go语言后的感受
- Python 计算程序运行时间
- javascript 延时执行函数
- PHP的几个常用加密函数【转载】
- Appium python自动化测试系列之页面滑动原理讲解(十)
- HTML5 拖放(Drag 和 Drop)详解与实例
- InnoDB的4个特性
- golang web实战之二(iris)
- [转]Windows下安装storm-0.9.1
- epoll+socket实现 socket并发 linux服务器
- Killing Monsters(hdu4970)
- Java 策略模式(Strategy)
- rhel7.x配置本地yum