typescript对象.assign()vs创建新对象效率

发布于 2025-01-21 20:24:12 字数 437 浏览 3 评论 0原文

我有两种方法可以实现一种恒定的方法,但是我不确定哪种方法更好。有人可以咨询吗?谢谢你!

方法1:放出一个常数,只能调用常数一次,但我需要返回一个新的变量

const X = {a: 0, b: 1, c: 2}
export class A{
   private method a (val: any) { 
       return {...X, ...val}
}}

方法2:在方法中放置一个常数,每次调用该方法都会调用常数(在我的方法中两次情况),但不需要创建一个新对象。

export class A{
   private method a (val: any) { 
       const x = {a: 0, b: 1, c: 2}
       return Object.assign(x, val)
}}

I have two ways to implement a method with a constant, but I am not sure which way is better. Can someone consult on it? Thank you!

method 1: put a constant out of the class, which will invoke the constant once only but I need to return a new variable

const X = {a: 0, b: 1, c: 2}
export class A{
   private method a (val: any) { 
       return {...X, ...val}
}}

method 2: put a constant in the method, which will invoke the constant every time calling the method (twice in my case) but does not need to create a new object.

export class A{
   private method a (val: any) { 
       const x = {a: 0, b: 1, c: 2}
       return Object.assign(x, val)
}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那一片橙海, 2025-01-28 20:24:12

我认为这里没有银色子弹,每种情况可能都不一样,但是我的建议是更多地关注变量的范围,而不是它叫多少时间或创建对象的次数。在第一个方法中,您在A类范围之外声明一个常数,因此,基本上,在同一范围内的任何类或功能都可以访问此变量,在您的情况下,这并不糟糕,因为它是一个常数,但是假设您已声明已声明带有LET的变量在这种情况下您的问题开始出现,原因是任何类或函数都可以用任何值重新分配此变量,从而在代码中引起副作用和错误,一个愚蠢的示例将澄清这些内容:

let x = 5

function isEven(value: number) {
    // here i'm overwriting the value of x
    x = 2

    return value % 2 === 0;
}

function destroyTheWorldIfEven(value: number) {
  if(isEven(value)){
      console.log("x is", x)
      console.log("you world has been destroyed")
      return
  }
  
  return console.log("you are safe")
}

// at the first call x is 5
destroyTheWorldIfEven(x)

// but at the second call x is 2 due the reassign in isEven function
destroyTheWorldIfEven(x)

因此,如果您不需要在功能或类之间共享const x,我认为没有任何理由将其保留在A类范围之外,我的建议是然后,将事物保留在其所属于的位置,然后:

  1. 如果const x仅在 A类A类方法中使用,请将其保留在方法中。
  2. 如果const x是由A类或多个方法使用的,请将其作为私有属性。
  3. 如果const x属于A类,但必须由其他类访问,请将其保留为公共属性,但避免将其定义为setter方法。

我将为您留下一些很好的参考:

javascript范围

javaScript HOISTING

javaScript封闭

固有

原理我认为重要的是,主要是当您使用OOP时,请务必阅读它。

I think there's no silver bullet here, each case may be different, but my suggestion is to pay more attention about the scope of the variable rather than how much time it's called or how many times you create the object. At the first method you are declaring a constant outside the scope of class A, so basically, any class or function at the same scope has access to this variable, in your case it's not that bad because it's a constant, but suppose you have declared a variable with let in such case your problems begin to arise, the reason is that any class or function can reassign this variable with any value, thus causing side effects and bugs in your code, a silly example will clarify the things:

let x = 5

function isEven(value: number) {
    // here i'm overwriting the value of x
    x = 2

    return value % 2 === 0;
}

function destroyTheWorldIfEven(value: number) {
  if(isEven(value)){
      console.log("x is", x)
      console.log("you world has been destroyed")
      return
  }
  
  return console.log("you are safe")
}

// at the first call x is 5
destroyTheWorldIfEven(x)

// but at the second call x is 2 due the reassign in isEven function
destroyTheWorldIfEven(x)

So if you don't need to share the const X among the functions or classes, i don't see any reason to keep it outside the scope of class A, my suggestion is to keep things where they belong to, then:

  1. if const x is only used in one method of class A, keep it within the method.
  2. if const x is used by one or more methods from class A, keep it as a private attribute.
  3. if const x belongs to class A but must be accessed by other classes, keep it as a public attribute but avoid to define a setter method to it.

I will leave some good references for you:

Javascript scope

Javascript hoisting

Javascript closure

SOLID principles

This last one is one of most important in my opinion, mainly when you are using OOP, be sure to read it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文