Socket
Socket
Sign inDemoInstall

@chenng/aop-js

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chenng/aop-js

Function Decorator


Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

aop-js

Function Decorator。

《JavaScript 设计模式与开发实践》中装饰者模式给出的例子。

使用

yarn add @chenng/aop-js
import '@chenng/aop-js';

例子

分离非业务代码

let showLogin = function() {
  console.log('打开登陆浮层');
}
let log = function() {
  console.log(`上报标签为:${this.getAttribute('tag')}`);
}
showLogin = showLogin.after(log); // 打开浮层上报数据
document.getElementById('button').onclick = showLogin;

动态改变函数参数

let ajax = function(type, url, params) {
  console.log(params);
}

ajax = ajax.before((type, url, params) => {
  params.Token = 'Token';
});

ajax('get', 'https://xxx.com/userinfo', {name: 'chenng'}); // {name: 'chenng', Token: 'Token'}

表单校验

const validata = function() {
  if (username.value === '') {
    alert('查询用户不能为空');
    return false;
  }
}

const formSubmit = function() {
  const param = {
    username: username.value
  }
  ajax('https://xxx.com/getUserInfo', param);
}

formSubmit = formSubmit.before(validata);

FAQs

Package last updated on 11 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc