每天一个JavaScript实例-apply和call的使用方法
2023-09-14 09:09:01 时间
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-apply和call的使用方法</title> <script> function Person(name,age){ //定义一个类,人类 this.name=name; //名字 this.age=age; //年龄 this.sayhello=function(){console.log("hello")}; } function Print(){ //显示类的属性 this.funcName="Print"; this.show=function(){ var msg=[]; for(var key in this){ if(typeof(this[key])!="function"){ msg.push([key,":",this[key]].join("")); } } console.log(msg.join("-----")); }; } function Student(name,age,grade,school){ //学生类 Person.apply(this,arguments); Print.apply(this,arguments); this.grade=grade; //年级 this.school=school; //学校 } var p1=new Person("jake",10); p1.sayhello(); var s1=new Student("tom",13,6,"清华小学"); s1.show(); s1.sayhello(); console.log(s1.funcName); </script> </head> <body> </body> </html>
相关文章
- Win10系列:JavaScript综合实例3
- [Rust] Load a WebAssembly Function Written in Rust and Invoke it from JavaScript
- [Javascript] Object.assign()
- [Javascript] Natively Format JavaScript Dates and Times
- [Javascript] Automate the process of flattening deeply nested arrays using ES2019's flat method
- [Javascript] Use a custom sort function on an Array in Javascript
- [Javascript] Object.freeze() vs Object.seal()
- 前端JavaScript规范
- JavaScript的语音识别
- 从零开始学_JavaScript_系列(20)——js系列<7>(函数原型的两种声明方式、函数的作用域)
- 〖大前端 - 基础入门三大核心之JS篇④〗- JavaScript中的数据类型之数字类型
- javascript基础修炼(11)——DOM-DIFF的实现