[ / / / / / / / / / / / / / ] [ dir / agatha2 / doomer / egy / g / roze / tingles / vichan / wmafsex ][Options][ watchlist ]

/tech/ - Technology

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Email
Comment *
File
Select/drop/paste files here
Password (Randomized for file and post deletion; you may also set your own.)
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

-- . .-. .-. -.-- / -.-. .... .-. .. ... - -- .- ... ---.

File (hide): ed1304874c856d6⋯.jpeg (9.26 KB, 259x194, 259:194, images.jpeg) (h) (u)

[–]

 No.1010462>>1010516 >>1010575 [Watch Thread][Show All Posts]

Hey n00b here.

I'm trying to get the variable from one javascript file and pass it on to another javascript file.

Like on page.js I create var x=1;

then on page2.js I do var y=x+1;

How do I do that?

 No.1010476

// module.mjs
export function hello() {
return "Hello";
}
// main.mjs
import ( hello ) from 'module'; // or './module'
let val = hello(); // val is "Hello";


 No.1010516>>1010575

>>1010462 (OP)

Presuming this is client-side JavaScript by the name of your files, all of your .js executes in the same global scope. It's considered bad practice to pollute the global scope with variables, so most frameworks will declare a single global variable to contain all of the bindings for your application. But if this is just some pet project, don't worry about it.


 No.1010575>>1011112 >>1011114

>>1010462 (OP)


// script_a.js
someVar = 140
// script_b.js
console.log(someVar + 60)
// page.html
<!-- ... -->
<script src='script_a.js'><\script>
<script src='script_b.js'><\script>
</body>
</html>

When you declare your variable with “var” you're restricting it to that scripts context.

Like >>1010516 said, it is bad practice to put too much stuff in global so don't overuse it.


 No.1011112

First >>1010575

but not using var would be an implicit global var which is invalid in strict mode (put '"use strict";' in the first line) so you should use window.x=1; for declaration.

The js files are run in the order they appear in in the HTML, so

<script src="page.js"></script>

<script src="page2.js"></script>

should work fine. No need for complicated extras or modules or classes or whatever.


 No.1011114>>1011275 >>1011346 >>1011359

File (hide): 129f1b6474fe6ab⋯.png (751.61 KB, 698x1000, 349:500, 129f1b6474fe6ab4867629b2bb….png) (h) (u)

And don't listen to the "muh polluting global" fags. They're framework fags who can't do anything. Be more like Terry. Short global names and vanilla js only.

>>1010575

><\script>

>\script

>\

Anon, I can't even...


 No.1011275>>1011425

>>1011114

Blame goyboard. For the “framework fags” argument, most people contextualise things as a debugging measure to prevent overwriting built-in browser variables, not because of frameworks.

>I can't even

Thanks tumblr


 No.1011346>>1011425

>>1011114

>not using mithril

It's like you hate fun.


 No.1011356

dom manipulation

js file 1: document.writeln() an add an element with a specific id

js file 2: getElementById()

don't want it to be visible? use css for the id and use display: none


 No.1011359>>1011425

>>1011114

no-coder detected.


 No.1011425

>>1011359

>>1011346

>>1011275

t. professional front end engineers


 No.1011440

SO HOW THE FUCK DO YOU DO IT?????????????????????????????????????????????


 No.1013138

If you declare it in global scope (inside no function) it will be accesible to the other file.

You could also make a global object like

let some_obj = {}

some_obj.x = 4

So you can put everything there as to not "contaminate" global scope too much.




[Return][Go to top][Catalog][Screencap][Nerve Center][Cancer][Update] ( Scroll to new posts) ( Auto) 5
12 replies | 1 images | Page ?
[Post a Reply]
[ / / / / / / / / / / / / / ] [ dir / agatha2 / doomer / egy / g / roze / tingles / vichan / wmafsex ][ watchlist ]