0
点赞
收藏
分享

微信扫一扫

机器学习:逻辑回归--过采样

天悦哥 2024-09-13 阅读 30

1.计算属性传参,还在return一个函数?

let nameFull = computed(() => {
    return e => {
        console.log('参数', e)
    }
})

那这样的话,干脆直接写一个函数

2.真正的计算属性传参,借助map实现

import { computed } from "vue";
export function zlcComputed(fn) {
  const map = new Map();
  return function (...args) {
    const key = JSON.stringify(args);
    if (map.has(key)) {
      return map.get(key);
    }
    const res = computed(() => {
      return fn(...args);
    });
    map.set(key, res);
    return res;
  };
}
<template>
  <div class="hello">{{ firstName("章") }}</div>
  <div class="hello">{{ firstName("龙") }}</div>
</template>

<script setup>
import { zlcComputed } from "@/utils/zlcComputed";
let firstName = zlcComputed((data) => {
  return data;
});
</script>

<style scoped>

</style>

3.有些人会有疑问,还不是每次都会走zlcComputed中return出来的函数?没错,但是万一设计到复杂的好性能的计算呢?如果使用步骤一的方法return一个函数,每次都会重新运行,而使用这种方式直接从map中取出结果返回,没有设计到计算。

举报

相关推荐

0 条评论