版权声明:原创不易,如需转载,请注明出处。有梦想地需要你的一个赞(顶)。 https://blog.csdn.net/genius_yym/article/details/81318161
关于对象函数的声明
简述
今天回顾了一下基础只是,顺带记录一下对象中声明函数的两种方式,感觉很鸡肋的样子。
一、在object中声明function
var a = {
b: 1994,
fun: function() {
return 1
}
}
a.fun(); //输出1
二、在function中声明function
function fun() {
//这里必须用this声明
this.funChild = function() {
return 2
}
}
var funObj = new fun();
funObj.funChild() //输出 2
但是
延伸
可以自定义自己的函数库,或者项目的函数库。类似,如下:
// 自定义的字典配置数据
const DictUtil = {};
DictUtil.getPlayStatus = function (statusCode) {
let status;
switch (statusCode) {
case 1:
status = '王者'
break;
case 2:
status = '坑逼'
break;
case 3:
status = '骂街'
break;
default:
status = '秀起来'
}
return status;
}
export default DictUtil;
---------------
// 使用方式:
//先声明DictUtil文件成组件,或者直接引入
import DictUtil from 'DictUtil';
console.log( DictUtil.getPlayStatus( Math.random() ) )
嗯哈~ 就这样吧。
结束语
知识无止境,继续加油。