Const variables access from one class to another class | JavaScript, Node.js, React.js and Angular.js Forum
S
Sivaranjani Sivakozhundhu Posted on 11/01/2021

Hello ,

I have doubt regarding Const Variables on how to use them from one class to another like the way function() works from one class to another.
Below is an example .
 
class1.js 
 
const company ="Company";
const lastName ="Last Name";
 
class lead_constants {
 
static get Company(){
return Company;
}
 
static get lastName (){
return lastName ;
}
 
}
 
module.exports =new lead_constants()
------------------------------------------------------------------------------------------------
class2.js
 
class LeadPage{
 
get_Company(){
return $('#username')
}
 
module.export -= new LeadPage()
 
---------------------------------------------------------------------
Class3.js
 
const lead_Page = require("./lead_Page")  
const lead_const = require("./lead_Constants")
 
class leadHelper{
lead_Page.Company   - works fine
lead_const.Company - doesn't work
---------------------------------------------------------------------------------------------------------
 
is there anyway to access the const variables or variables from one class to another ?

Y
Yogesh Chawla Replied on 11/01/2021

 

Let's talk about methods first:

 

If it is a static method (doesn't use any instance data), then declare it as a static method and you can directly call it.

If it's an instance method then you can create an object of type one and then call the method on that object (usually in the constructor).

To make the method static :

class One {
static write(){
console.log("Yes! I did!");
}
}

class Two {
tryingMethod(){
One.write();
}
}

 

For the non-static method, you need to create the instance of the One object in a constructor for Two like this:

 

class One {
write(){
console.log("Yes! I did!");
}
}

class Two {
constructor() {
this.one = new One();
}

tryingMethod(){
this.one.write();
}
}

var x = new Two();
x.tryingMethod();

 

Now Let's talk about const keyword:

a variable in the global scope should be accessible to all scripts loaded after it is declared.

// first.js
var colorCodes = {

back : "#fff",
front : "#888",
side : "#369"

};

... in another file ...
// second.js
alert (colorCodes.back); // alerts `#fff`

... in your html file ...
<script type="text/javascript" src="first.js"></script>
<script type="text/javascript" src="second.js"></script>


You can also export the variable from first file using export.

//first.js
const colorCode = {
black: "#000",
white: "#fff"
};
export { colorCode };
Then, import the variable in second file using import.

//second.js
import { colorCode } from './first.js'


Check this. This will help:


Using Node.js you can export the variable via module.

 

//first.js
const colorCode = {
black: "#000",
white: "#fff"
};
module.exports = { colorCode };

Then, import the module/variable in second file using require.

//second.js
const { colorCode } = require('./first.js')

 

This can be used too

Export a const from the module

export const constant1 = 33;

And import that from the module where necessary. Or, building on your static method idea, you could declare a static get accessor:

const constant1 = 33, constant2 = 2;


class Example {

static get constant1() {
return constant1;
}

static get constant2() {
return constant2;
}
}


That way, you won't need parenthesis:

const one = Example.constant1;

 

 

 

Proposed in ES7:

Since a class is just syntactic sugar for a function, you can just add a non-writable property like so:

class Example {
}

Object.defineProperty(Example, 'constant1', {
value: 33,
writable : false,
enumerable : true,
configurable : false
});

Example.constant1; // 33
Example.constant1 = 15; // TypeError

It may be nice if we could just do something like:

class Example {
static const constant1 = 33;
}

Fortunately this class property syntax is only in an ES7 proposal.

 

 

In your case, there might be something wrong with the getter method in lead_constants. May be that's why it is not picking.

If this understanding doesn't help, please paste your code in the mail. I will make sure, it runs.


Y
Yogesh Chawla Replied on 19/01/2021

First.js File

const first = {

    name: 'Yogesh',
    language: 'JS'
};

module.exports = {first};
 
Second.js File
 
const sourceFile = require('./First.js');
console.log(sourceFile.first.name);
 
This you can use to access const variable from one file into another file in JS.