最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Convert var to const javascript - Stack Overflow

programmeradmin2浏览0评论

Is it possible to convert a var to a const?

Say in a scenario like this:

var appname = ''

function setAppName(name) {
   appname = name  // can I convert this to const now?
}

Basically, this is for a parameter level. Parameter needs to be accessible throughout the file and I want to make it const after first assignment. Is this possible?

To add: If there is any other way besides creating an object with to contain this parameter, it would be better (for a more straightforward solution). I will be working on multiple parameters like this and freezing an object will cause the entire object to freeze. I am hoping to do this on the parameter level.

So either change the parameter to become const. OR (this one I dont think is possible) change the scope of the paramter to global:

function setAppName(name) {
   const appname = name  // can I change the scope and make this global?
}

Thanks.

Is it possible to convert a var to a const?

Say in a scenario like this:

var appname = ''

function setAppName(name) {
   appname = name  // can I convert this to const now?
}

Basically, this is for a parameter level. Parameter needs to be accessible throughout the file and I want to make it const after first assignment. Is this possible?

To add: If there is any other way besides creating an object with to contain this parameter, it would be better (for a more straightforward solution). I will be working on multiple parameters like this and freezing an object will cause the entire object to freeze. I am hoping to do this on the parameter level.

So either change the parameter to become const. OR (this one I dont think is possible) change the scope of the paramter to global:

function setAppName(name) {
   const appname = name  // can I change the scope and make this global?
}

Thanks.

Share Improve this question edited Jul 6, 2020 at 9:38 kzaiwo asked Jul 6, 2020 at 9:27 kzaiwokzaiwo 1,7683 gold badges22 silver badges62 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 14

Put your app name in an object and freeze it.

let appSettings = { name: "" };
function setAppName(name) {
    appSettings.name = name;
    Object.freeze(appSettings);
}

Freezing prevents adding, removing and modifying the values of the properties in your object so you should call Object.freeze only once after all other settings (if there are more variables you want to make constant) have been set.

You can do this using a object.

const globals = {
    appname: ''
}

function setAppName(name) {
    globals.appname = name;
    Object.freeze(globals)
    // After this point, value of appname cannot be modified
}

Thank you for all your inputs.

I was able to find a workaround for what I was trying to achieve.

var appname = ''

function setAppName(name) {
  if (appname === '') {
    appname = name  // can I convert this to const now?
  }
}

Although this doesnt convert it to const, I just added a guard on the setter and it will not be able to overwrite the value now (unless otherwise I am going to initialize again to an empty string).

It is not fool-proof, but this will address my need.

Thanks all!

发布评论

评论列表(0)

  1. 暂无评论