逆推继承看原型 函数的角色 函数声明和函数表达式的区别 函数中this指向的问题
生活随笔
收集整理的這篇文章主要介紹了
逆推继承看原型 函数的角色 函数声明和函数表达式的区别 函数中this指向的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
逆推繼承看原型
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>function F1(age){this.age = age;}function F2(age){this.age = age;}F2.prototype = new F1(10);function F4(age){this.age = age;}F4.prototype = new F2(20);var f4 = new F4(40);console.log(f4.age);</script> </head> <body></body> </html>函數的角色
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 函數的角色:// 函數的聲明function f1(){console.log("我是函數");}f1();// 函數表達式var ff = function(){console.log("我也是一個函數");};ff();</script> </head> <body></body> </html>?
函數聲明和函數表達式的區別
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 函數聲明 // if(true){// function f1(){// console.log("哈哈,我又變帥了");// }// }else{// function f1(){// console.log("小蘇好猥瑣");// }// }// f1();// 函數表達式var ff;if(true){ff = function(){console.log("哈哈,我又變帥了");};}else{ff = function(){console.log("小蘇好猥瑣");};}ff();// 函數聲明如果放在if-else的語句中,在IE8的瀏覽器中會出現問題// 以后寧愿用函數表達式,都不用函數聲明var ff = function(){};</script> </head> <body></body> </html>函數中this指向的問題
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>/*** 函數中this的指向* 普通函數中的this是誰?----window* 對象.方法中的this是誰?----當前的實例對象* 定時器方法中的this是誰?----window* 構造函數中的this是誰?----實例對象* 原型對象方法中的this是誰?----實例對象* * */// 嚴格模式:"use strict";function f1(){// windowconsole.log(this);}window.f1();// var obj = {// sayHi:function(){// }// };// 普通函數// function f1(){// console.log(this);// }// f1();// BOM中頂級對象是window,瀏覽器中所有的東西window的// 定時器中的this// setInterval(function(){// console.log(this);// },1000);// function Person(){// // console.log(this);// this.sayHi = function(){// console.log(this);// };// }// Person.prototype.eat = function(){// console.log(this);// };// var per = new Person();// console.log(per);// // per.sayHi();// per.eat();// 普通函數// 構造函數// 對象的方法</script> </head> <body></body> </html>?
總結
以上是生活随笔為你收集整理的逆推继承看原型 函数的角色 函数声明和函数表达式的区别 函数中this指向的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis 批量将list数据插入到
- 下一篇: 函数的不同的调用方式 函数也是对象 数组