當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
javascript函数调用的几种方式
生活随笔
收集整理的這篇文章主要介紹了
javascript函数调用的几种方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?function fn() {console.log(this.name);return "fn函數(shù)的返回值";}/*1.方法調(diào)用*///方法調(diào)用,this指向window,相當(dāng)于是window調(diào)用fn();/*2.對象調(diào)用*/var obj={name:"zhangsan",fun:function(){console.log(this.name+"zhangsan的方法");}};obj.fun();//函數(shù)中的this指向obj/*3.構(gòu)造函數(shù)調(diào)用*/var fns = new fn();//函數(shù)中的this指向函數(shù)的實例也就是fns//對于構(gòu)造函數(shù)的返回值問題/*1.如果構(gòu)造函數(shù)沒有return語句,返回值默認(rèn)為實例對象* 2.如果return 基本類型數(shù)據(jù);返回值為實例對象* 3.如果return 對象;返回值為return中的對象* */console.log("****************");/*4.上下文調(diào)用模式call() apply()*/var obj4={say:function(){console.log("說話的方法");}};var obj5 ={name:"zhangsan"};//obj5對象想使用obj4的say方法obj4.say.call(obj5);/** call(對象,參數(shù)1,參數(shù)2....)* apply(對象,[參數(shù)1,參數(shù)2,參數(shù)3])* */?
轉(zhuǎn)載于:https://www.cnblogs.com/itlyh/p/6045777.html
總結(jié)
以上是生活随笔為你收集整理的javascript函数调用的几种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 3449 有依赖性的01背包
- 下一篇: 多线程同步的方法