Utilize symbols with specified properties
P粉517090748
P粉517090748 2023-09-18 12:45:11
0
1
404

In the code shown below, I have defined some constants. I want to useSymbolto ensure each constant is unique. But when I use the following line of code:

if (isBtnDigitizePolygonClicked.value == true) { return polygDigiConstants.CONST_STRING_DIGITIZE; }

The value returned by the above code isSymbol('Digitize'), but I expected it to beDigitizeas described in this tutorial:https://www. scaler.com/topics/enum-in-javascript/

Tutorial content:

const Direction = Object.freeze({ North: Symbol('north'), East: Symbol('east'), West: Symbol('west'), South: Symbol('south'), }) const Pole = Object.freeze({ North: Symbol('north'), South: Symbol('south'), }) console.log(Direction.North === Pole.North) 上述代码的输出为: false

Please tell me how to useSymbolcorrectly to define properties.

polygDigiConstants.js

function define(name, value) { Object.defineProperty(polygDigiConstants, name, { value: value, enumerable: true, writable: false, }); } export let polygDigiConstants = {}; define('CONST_STRING_DIGITIZE', Symbol('Digitize')); define('CONST_STRING_STOP_DIGITIZE', Symbol('Stop')); define('CONST_STRING_CLEAR_DIGITIZED', Symbol('Clear'));
P粉517090748
P粉517090748

reply all (1)
P粉200138510

polygDigiConstants.js

function define(name, value) { Object.defineProperty(polygDigiConstants, name, { value: value, enumerable: true, writable: false, }); } export let polygDigiConstants = {}; define('CONST_STRING_DIGITIZE', Symbol('Digitize')); define('CONST_STRING_STOP_DIGITIZE', Symbol('Stop')); define('CONST_STRING_CLEAR_DIGITIZED', Symbol('Clear'));

JS

import { polygDigiConstants } from './polygDigiConstants.js'; if (isBtnDigitizePolygonClicked.value == true) { return polygDigiConstants.CONST_STRING_DIGITIZE.description; // 这将给你 'Digitize' } function define(name, value) { Object.defineProperty(polygDigiConstants, name, { value: value, enumerable: true, writable: false, }); } export let polygDigiConstants = {}; define('CONST_STRING_DIGITIZE', 'Digitize'); define('CONST_STRING_STOP_DIGITIZE', 'Stop'); define('CONST_STRING_CLEAR_DIGITIZED', 'Clear');

polygDigiConstants.CONST_STRING_DIGITIZE will give you the string 'Digitize' directly.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!