es6之decorator
来源:互联网 发布:淘宝的追加评论在哪里 编辑:程序博客网 时间:2024/06/08 06:29
定义:decorator是一个函数,修改行为,修改类的行为,只在类的范畴内有用
扩展:
第三方库修饰器的js库:core-decorators; npm install core-decorators
就不用自己手动的写readonly,typename等,只需要import引入就可以使用
//以下为代码实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
console.log(
"decorator行为"
);
//只读属性,将time变为只读属性
let
readonly=
function
(target,name,descriptor){
descriptor.writable=
false
;
return
descriptor
}
class
Test{
@readonly //第一种用法:在类的里面使用,引入修饰器,修改time()的行为
time(){
return
"2017-03-11"
}
}
let
test=
new
Test();
// test.time="reset time"; //修饰器设置的只读属性,不能进行修改会报错
console.log(test.time());
}
{
let
typename=
function
(target,name,descriptor){
target.myname=
"hello"
}
@typename//第二种用法:在类的外面使用
class
Test{
}
console.log(
"类修饰符"
,Test.myname);
}
//埋点,做日志统计
letlog=(type)=>{
return function(target,name,descriptor){
letsrc_method=descriptor.value;
descriptor.value=(...arg)=>{
src_method.apply(target,arg);
console.info(`log${type}`);//实现埋点,真实的业务中直接换成一个接口就可以了
}
}
};
classAD{
@log('show')
show(){
console.log('adisshow');
};
@log('click')
click(){
console.log('adisclick');
}
}
letad=newAD();
ad.show();
ad.click();
数据埋点分为初级、中级、高级三种方式,分别为:
初级:在产品、服务转化关键点植入统计代码,据其独立ID确保数据采集不重复(如购买按钮点击率);
中级:植入多段代码,追踪用户在平台每个界面上的系列行为,事件之间相互独立(如打开商品详情页——选择商品型号——加入购物车——下订单——购买完成);
高级:联合公司工程、ETL采集分析用户全量行为,建立用户画像,还原用户行为模型,作为产品分析、优化的基础。
阅读全文
0 0
- es6之decorator
- 学习笔记:ES6之Decorator--修饰器
- es6 Decorator(十六)
- 设计模式之Decorator
- 设计模式之 Decorator
- 设计模式之Decorator
- 设计模式之Decorator
- 设计模式之Decorator
- 设计模式之 decorator
- 设计模式之Decorator
- 设计模式之Decorator
- 设计模式之Decorator
- 设计模式之Decorator(油漆工)
- 设计模式之Decorator(油漆工)
- 设计模式之Decorator(油漆工)
- 设计模式之Decorator(油漆工)
- 设计模式之Decorator(油漆工)
- 设计模式之Decorator(油漆工)
- drag confirm
- java if else嵌套代码重构
- Android Studio 升级到3.0遇到的坑
- List去重
- mysql数据库sql语句集锦
- es6之decorator
- 冒泡排序
- java 核心代码加密思路
- 子类与继承
- Ionic3项目开发——发起定向HTTP请求
- 阿里云主机配置HTTPS
- 解决cannot find -lopencv_dep_cudart问题
- 算法推导与案例积累的方法
- 大学生活随笔