文章目录
一、函数嵌套函数调用方法
function xxx(){
let hex_md5 = function () {
console.log("XiaoHao")
}
}
此时如何调用hex_md5
呢,其实将hex_md5
方法导出即可。
1.1、外层定义变量导出
let _hex_md5;
function xxx(){
let hex_md5 = function () {
console.log("XiaoHao")
}
_hex_md5 = hex_md5;
}
xxx();
_hex_md5();
// 因为js是边解析边运行,所以要先运行xxx方法,
// 这样hex_md5才会赋值给_hex_md5.
1.2、将方法改为自执行
let _hex_md5;
!(function(){
let hex_md5 = function () {
console.log("XiaoHao")
}
_hex_md5 = hex_md5;
})()
_hex_md5()
二、函数嵌套对象调用方法
function xxx() {
let j = {
hex_md5: function (){
console.log("XiaoHao")
}
}
}
如上和函数嵌套函数一样, 都需要将想要执行的方法导出
// 变量导出
let _hex_md5;
function xxx() {
let j = {
hex_md5: function (){
console.log("XiaoHao")
}
}
_hex_md5 = j
}
xxx()
_hex_md5.hex_md5()
// 自执行
let _hex_md5;
!(function() {
let j = {
hex_md5: function (){
console.log("XiaoHao")
}
}
_hex_md5 = j
})()
_hex_md5.hex_md5()
Js中不同方式定义的方法
1.普通型: var XiaoHao = function(){}
2.对象型: XiaoHao: function(){}
3.property方式: XiaoHao.property.XXX = function() --用new进行定义对象。
三、webpack方法调用
3.1、webpack形式
!(function(x){
function xx(yy){
x[yy].call('1', '2', '3'); // 必须有一个加载模块的方法 call apply
}
xx(0)
})([function (a, b ,c){}, function (){}])
3.2 扣webpack js步骤
- 找到这个加载器(加载模块的方法)
- 找到要调用的模块
- 构造一个自执行方法
- 导出调用函数
- 编写自定义方法按照流程加密
3.3 webpack模板赋值示例
3.3.1 扣出加载器和加载对象
!function(t) {
var s = {}
function e(s) {
var i = {}
var n = i[s] = {
exports: {},
id: s,
loaded: !1
};
return t[s].call(n.exports, n, n.exports, e),
n.loaded = !0,
n.exports
};
_n = e
}()
3.3.2 扣出要使用的方法添加到加载器括号中。
3.3.3 导出加载器,编写调用函数
找到这个加载器(加载模块的方法) & 构造一个自执行方法
如图: 只将绿色代码块扣下即可。
找到要调用的模块
向上寻找属于哪一层级即可找到对应的代码块。
注意: 断点过程中遇到then
函数要在then
函数上下断点
四、函数的导出
将对象传入函数并进行操作。
var xiaohao = {}
!(function a(exports){
exports.zhang='123'
var Jsencode = function(){}
Jsencode.prototype.get_pwd = function(){}
exports.jsencode = Jsencode;
})(xiaohao)
var jsenv = xiaohao.jsencode;
//此时可以new jsenv就可以把对象导出适用对象。
var ex = new jsenv()
xiaohao.zhang
// 123
使用中间变量导出
var jiami;
!(function a(){
var Jsencode = function(){}
Jsencode.prototype.get_pwd = function(){}
jiami = Jsencode;
})()
// 此时可以new jiami就可以把对象导出适用对象。
var jiami = new jiami()