gpt4 book ai didi

javascript es6模块变量与静态

转载 作者:行者123 更新时间:2023-11-29 11:00:21 25 4
gpt4 key购买 nike

假设我们有这 2 个类,假设 doSomething 不需要 this访问。我能看到的唯一区别是,模块“更私密”,还有什么别的吗?

示例1.js

class Sample1 extends React.Component {
static doSomething(input) {
// ...
return input2;
}

render() {
if (Sample1.doSomething(x)) {
return <div />;
}
return null;
}
}
export { Sample1 };

示例2.js

const doSomething = (input) => {
// ...
return input2;
};

class Sample2 extends React.Component {
render() {
if (doSomething(x)) {
return <div />;
}
return null;
}
}
export { Sample2 };

最佳答案

嗯,在Sample1中doSomething是暴露的,你可以通过Sample1.doSomething()来访问它,但是在Sample2场景中,这个方法在外部是不可用的模块作用域,是的,它是私有(private)的。

现在,关于内存消耗,无论您创建多少个实例,静态方法都会分配一次,并且只能通过类访问,不能通过实例访问。

这对你来说有意义吗?

关于javascript es6模块变量与静态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48198162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com