函数的不同的调用方式 函数也是对象 数组的函数调用 apply和call方法的使用
生活随笔
收集整理的這篇文章主要介紹了
函数的不同的调用方式 函数也是对象 数组的函数调用 apply和call方法的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
函數的不同的調用方式
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 普通函數function f1(){console.log("文能提筆控蘿莉");}f1();// 構造函數----通過new來調用,創建對象function F1(){console.log("我是構造函數");}var f = new F1();// 對象的方法function Person(){this.play = function(){console.log("玩代碼");};}var per = new Person();per.play();</script> </head> <body></body> </html>函數也是對象
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 函數也是對象,對象不一定是函數// 對象中有__proto__原型,是對象// 函數中有prototype原型,是對象// function F1(){// }// console.dir(F1);// Math中有__proto__,但是沒有prototypeconsole.dir(Math);// 對象中有__proto__,函數中應該有prototype// 如果一個東西里面有prototype,又有__proto__,說明是函數,也是對象// function F1(name){// this.name = name;// }// console.dir(F1);// 所有的函數實際上都是Function的構造函數創建出來的實例對象// 所以,函數實際上也是對象// console.dir(f1);// var f1 = new Function("num1","num2","return num1+num2");// console.log(f1(10,20));// console.log(f1.__proto__==Function.prototype);// function f1(num1,num2){// return num1 + num2;// }// var f2 = function(num1,num2){// return num1 + num2;// }console.dir(Function);</script> </head> <body></body> </html>數組的函數調用
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 數組可以存儲任何類型的數據// function f1(){// }// console.log(typeof f1);var arr = [function(){console.log("十一假期快樂");},function(){console.log("十一假期開心");},function(){console.log("十一假期健康");},function(){console.log("十一假期安全");}];// 回調函數:函數作為參數使用arr.forEach(function(ele,index,array){ele();});</script> </head> <body></body> </html>apply和call方法的使用
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// apply和call的使用// 作用:可以改變this的指向// "use strict";function f1(x,y){console.log("結果是:"+(x+y)+this);return 100;}// 函數的調用// f1(10,20);console.log("=============");// 此時的f1實際上是當成對象來使用的,對象可以調用方法// apply和call方法也是函數的調用方式// f1.apply();// f1.call();console.log("==============");// f1.apply(null);// f1.call(null);// apply和call方法中如果沒有傳入參數,或者是傳入的是null,那么調用該方法的// 函數對象中的this就是默認的window// f1.apply(null,[100,200]);// f1.call(null,100,100);// apply和call都可以讓函數和方法來調用,傳入參數和函數自己的寫法不一樣// 但是效果是一樣的// var result1 = f1.apply(null,[10,20]);// var result2 = f1.call(null,10,20);// console.log(result1);// console.log(result2);// function f1(x,y){// console.log("這個函數是window對象的一個方法"+(x+y)+this.sex);// }// window.f1(10,20);// // obj是一個對象// var obj = {// age:10,// sex:"男"// };// window.f1.apply(obj,[10,20]);// window.f1.call(obj,10,10);// console.dir(obj);// apply和call可以改變this的指向function Person(age,sex){this.age = age;this.sex = sex;}// 通過原型添加方法Person.prototype.sayHi = function(){console.log("您好:" + this.sex);return 100;};var per = new Person(10,"男");per.sayHi();console.log("=========================");function Student(name,sex){this.name = name;this.sex = sex;}var stu = new Student("小明","人妖");var r1 = per.sayHi.apply(stu,[10,10]);var r2 = per.sayHi.call(stu,10,50);console.log(r1);console.log(r2);</script> </head> <body></body> </html>?
總結
以上是生活随笔為你收集整理的函数的不同的调用方式 函数也是对象 数组的函数调用 apply和call方法的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 逆推继承看原型 函数的角色 函数声明和函
- 下一篇: 总结apply和call方法的使用 bi