Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
DriedSponge

Export a variable from within a class TS

Recommended Posts

Posted · Original PosterOP

Is it possible to export a variable in typescript from within a class? For example:
Instead of:

export const variable = "Test";

I could do:
 

class Test{
	constructor(){
		export const variable = "test";
	}
}

Thanks in advance.


Remember to quote or @mention others, so they are notified of your reply

Desktop:

AMD Ryzen 7 3800X | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 16 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

SK hynix Gold S31 500GB SSDSeagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Noctua NF-A14 PWM 140 mm 

Phanteks AMP 650 W 80+ Gold Certified Modular  | Phanteks Eclipse P300A Mesh Mid Tower | Windows 10 Home

Link to post
Share on other sites

Why?


|| ℍ𝕚 𝕙𝕠𝕨 𝕒𝕣𝕖 𝕪𝕒 || 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites
Posted · Original PosterOP
22 minutes ago, Slottr said:

Why?

Trying to eliminate the need for the parseCollection function I have:
 

class SpongeHelpers{
    public this: this;
    constructor(sel) {
        SpongeHelpers.prototype.this = this;
        if (!sel) return;
        if (typeof sel === 'function') {
            document.addEventListener('DOMContentLoaded', sel);
        }else if (typeof sel === 'string') {
            return parseCollection.apply(document.querySelectorAll(sel));
        }

    }
    init(sel){
        return new SpongeHelpers(sel);
    }
}
//var sh;
window['$'] = SpongeHelpers.prototype.init;
const sh = SpongeHelpers.prototype.init;
var parseCollection = function(){
    parseCollection.prototype.this = this;
    return parseCollection.prototype;
};
export const col = parseCollection.prototype;


 


Remember to quote or @mention others, so they are notified of your reply

Desktop:

AMD Ryzen 7 3800X | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 16 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

SK hynix Gold S31 500GB SSDSeagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Noctua NF-A14 PWM 140 mm 

Phanteks AMP 650 W 80+ Gold Certified Modular  | Phanteks Eclipse P300A Mesh Mid Tower | Windows 10 Home

Link to post
Share on other sites
3 hours ago, DriedSponge said:

Is it possible to export a variable in typescript from within a class? For example:
Instead of:


export const variable = "Test";

I could do:
 


class Test{
	constructor(){
		export const variable = "test";
	}
}

Thanks in advance.

export let variable;

class Test{
	constructor(){
		variable = "test";
	}
}


though this is bad practise you should have a public variable in the class you can access.


                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites
Posted · Original PosterOP
6 hours ago, vorticalbox said:

export let variable;

class Test{
	constructor(){
		variable = "test";
	}
}


though this is bad practise you should have a public variable in the class you can access.

I will expierement with this, thank you!


Remember to quote or @mention others, so they are notified of your reply

Desktop:

AMD Ryzen 7 3800X | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 16 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

SK hynix Gold S31 500GB SSDSeagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Noctua NF-A14 PWM 140 mm 

Phanteks AMP 650 W 80+ Gold Certified Modular  | Phanteks Eclipse P300A Mesh Mid Tower | Windows 10 Home

Link to post
Share on other sites
51 minutes ago, DriedSponge said:

I will expierement with this, thank you!

again mutable exports are not advised.


                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites
Posted · Original PosterOP
1 minute ago, vorticalbox said:

again mutable exports are not advised.

Noted 


Remember to quote or @mention others, so they are notified of your reply

Desktop:

AMD Ryzen 7 3800X | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 16 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

SK hynix Gold S31 500GB SSDSeagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Noctua NF-A14 PWM 140 mm 

Phanteks AMP 650 W 80+ Gold Certified Modular  | Phanteks Eclipse P300A Mesh Mid Tower | Windows 10 Home

Link to post
Share on other sites

You can also make this a public static field on the class, which would be a more idiomatic way to do this I believe

export class Test {
    public static myVar: string = "asdf";
}

It's possible, although generally bad practice, to assign to that from the constructor.

 

You can then use it as

import {Test} from "./test"

Test.myVar // asdf

 

If you want to have a different value per instance of the class, or are able to access it from an instance of the class rather than the class itself, you would not make it static:

export class Test {
    public myVar: string;
    constructor() {
        myVar = "asdf";
    }
}

let test = new Test();
test.myVar; // asdf

 


HTTP/2 203

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×