js 高阶函数 AOP 切片编程 重写数组原型方法

概念

AOP(面向切片编程) 就是把一些与业务逻辑模块无关的功能抽离出来,然后通过“动态织入”的方式参入业务的逻辑模块中。这样设计的好处是,首先可以保证业务逻辑模块的纯净和高内聚性;其次可以方便的复用模块。

示例

function say(who){
    console.log(`${who} 说话`)
}

Function.prototype.before = function(callback){
    return (who)=>{
        callback()
        this(who) // 指向say
    }
}

let newSay = say.before(function(){
    console.log('先刷牙')
})
newSay('张三')

/**
 * 重写数组原型方法
 * push shift unshift pop reverse sort splice 会导致数组本身变化
 */

const oldArrayMethods = Array.prototype;

// 创建对象 原型指向oldArrayMethods
export const arrayMethods = Object.create(oldArrayMethods)

const methods = [
    'push',
    'shift',
    'unshift',
    'pop',
    'reverse',
    'sort',
    'splice'
]

// AOP 切片编程
methods.forEach(methods => {
    arrayMethods[methods] = function (...arg) {
        // 调用Array原型方法
        const res = oldArrayMethods[methods].apply(this, arg)

        // push unshift 添加的元素可能还是一个对象
        let inserted; // 单前添加的元素
        switch(methods){
            case 'push':
            case 'unshift':
                inserted = arg
                break;
            case 'splice': // arr.splice(0,1,2)
                inserted = arg[2]
                break;
        }
        // 观察新的值
        // if(inserted) this.__ob__.observerArray(inserted);
        
        return res
    }
})

// --------------------

// 重写数组方法 修改原型
arr.__proto__ = arrayMethods
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot实现AOP面向切面编程的具体方法如下: 1. 首先,你需要在项目的pom.xml文件中添加spring-boot-starter-aop依赖。可以参考以下代码: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 然后,你需要编写一个用于拦截的bean。这个bean将包含你想要在目标方法执行前后执行的逻辑。你可以使用注解或者编程方式来定义切面。例如,你可以使用@Aspect注解来定义一个切面,然后在切面的方法上使用@Before、@After等注解来定义具体的拦截行为。 3. 接下来,你需要将切面应用到目标对象上,创建代理对象。这个过程称为织入(Weaving)。在Spring Boot中,你可以使用@EnableAspectJAutoProxy注解来启用自动代理,它会根据切面定义自动创建代理对象。 总而言之,Spring Boot实现AOP面向切面编程的具体方法包括:添加依赖、编写用于拦截的bean,以及启用自动代理。这样就能实现在目标方法执行前后执行特定逻辑的效果了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合aop面向切面编程过程解析](https://download.csdn.net/download/weixin_38689551/12743012)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot实现AOP面向切面编程](https://blog.csdn.net/weixin_52536274/article/details/130375560)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值