|
Hey here comes a really feeble and probabaly hard to understand JS question but I can't figure out what I need to type to find similar problems. I'm using an object to store data to be used to generate new objects dynamically. (backstory: I'm using pixiJS and cant stringify the pixijs objects) dataModel.object = {name: "object", otherVariable=0} newObject = {} newObject.name = dataModel.object.name How could I change otherVariable on dataModel.object by referencing newObject? I tried adding a value to dataModel.object ( DM: dataModel.object ) so I could then go newObject.DM = dataModel.object.DM newobject.DM.othervariable += 1 but DM gets undefined, probably to prevent crashes. I know I could do it using eval but I'd rather not. Cheers EDIT: I think I got around this by just transferring all the data to newObject, deleting dataModel.object and then recreating dataModel.object as and when required (for stringify/saving) A LOVELY LAD fucked around with this message at 22:23 on Sep 11, 2015 |
# ? Sep 11, 2015 19:29 |
|
|
# ? Jun 1, 2024 05:48 |
|
A LOVELY LAD posted:Hey here comes a really feeble and probabaly hard to understand JS question but I can't figure out what I need to type to find similar problems. If by this you mean you wrote something like JavaScript code:
JavaScript code:
JavaScript code:
|
# ? Sep 11, 2015 22:32 |
|
qntm posted:
I did try it that way originally but then I can't dynamically store/set up the DM bit when stringifying the object, I would have to make an array of these DM references or something similar I think.
|
# ? Sep 12, 2015 09:29 |
|
Got another stupid question. What are strings in javascript encoded as? Are they by default UTF-8?
|
# ? Sep 14, 2015 11:47 |
|
It depends on the engine but generally it's UTF-16.
|
# ? Sep 14, 2015 12:23 |
|
Is it best to use XMLHttpRequest or JSONP? Book I'm reading (Head First HTML5) kinda leans towards JSONP but is not explicit.
|
# ? Sep 15, 2015 06:21 |
|
three posted:Is it best to use XMLHttpRequest or JSONP? Book I'm reading (Head First HTML5) kinda leans towards JSONP but is not explicit. I don't see why anyone would prefer JSONP over XHR if the job can be done with XHR. JSONP requires trusting the server and executing arbitrary javascript code.
|
# ? Sep 15, 2015 08:13 |
|
Knifegrab posted:Got another stupid question. What are strings in javascript encoded as? Are they by default UTF-8? JavaScript as standardized expects UCS2-with-surrogates in source files (it seemed like a good idea in 1997). Engines use a variety of encodings internally, with most engines having at least two depending on the contents of the string.
|
# ? Sep 15, 2015 10:11 |
|
I need some help with callbacks as I don't really understand them. I have this code which gets a string from a url, and then sends an alert with the text. I don't want the alert to show until the string is returned (long running database procedure), but I also don't want to block the UI.code:
|
# ? Sep 15, 2015 16:21 |
|
Put the alert(resp); in the callback itself.
|
# ? Sep 15, 2015 16:32 |
|
Uziel posted:I need some help with callbacks as I don't really understand them. I have this code which gets a string from a url, and then sends an alert with the text. I don't want the alert to show until the string is returned (long running database procedure), but I also don't want to block the UI. This is the order in which your code runs:
|
# ? Sep 15, 2015 16:37 |
|
Thanks! That was easier than I thought:code:
|
# ? Sep 15, 2015 17:04 |
|
So I've always just had one big main clientside javascript file per page. Now that I am doing something more complex I feel like my single page approach is getting far too unwieldy. Obviously on the serverside with node and requires its easy to build complex systems with multiple files, but I really am scratching my head figuring out the best approach here for client side. Anyone have any ideas?
|
# ? Sep 15, 2015 17:13 |
|
Knifegrab posted:So I've always just had one big main clientside javascript file per page. Now that I am doing something more complex I feel like my single page approach is getting far too unwieldy. Write everything in different files and use ES6 style imports, then use webpack to pack them together in one file to be linked from the HTML.
|
# ? Sep 15, 2015 18:25 |
|
Do you want to edit in multiple source files, or serve different script to different pages based on what they need? Webpack works well for the former, don't bother with the latter.
|
# ? Sep 15, 2015 18:30 |
|
Subjunctive posted:Do you want to edit in multiple source files, or serve different script to different pages based on what they need? Webpack works well for the former, don't bother with the latter. I just want to edit multiple source files, for management reasons I feel it would be a lot easier to have mutliple tabs open than to scroll up and down a gigantic file. I only have one true "webpage" so I don't have to worry about the latter.
|
# ? Sep 15, 2015 19:03 |
|
Knifegrab posted:I just want to edit multiple source files, for management reasons I feel it would be a lot easier to have mutliple tabs open than to scroll up and down a gigantic file. I only have one true "webpage" so I don't have to worry about the latter. Well, if that is ALL you are looking for, VSCode & Atom editors allow side by side editing within the same file.
|
# ? Sep 15, 2015 19:53 |
|
Skandranon posted:Well, if that is ALL you are looking for, VSCode & Atom editors allow side by side editing within the same file. I use atom, though I appreciate the suggestion as Atom does indeed rule. However I would still like to split my work across multiple files if possible.
|
# ? Sep 16, 2015 01:54 |
|
Knifegrab posted:I use atom, though I appreciate the suggestion as Atom does indeed rule. However I would still like to split my work across multiple files if possible. Webpack.
|
# ? Sep 16, 2015 02:20 |
|
piratepilates posted:Write everything in different files and use ES6 style imports, then use webpack to pack them together in one file to be linked from the HTML. I just looked up es6 style imports and its not yet supported by firefox so I think I had better hold off on using that for now.
|
# ? Sep 16, 2015 04:20 |
|
Knifegrab posted:I just looked up es6 style imports and its not yet supported by firefox so I think I had better hold off on using that for now. One thing you can do with typescript is compile multiple ts files into one big js file, and just serve that as a single script.
|
# ? Sep 16, 2015 04:36 |
|
Knifegrab posted:I just looked up es6 style imports and its not yet supported by firefox so I think I had better hold off on using that for now. No no, use ES6 style imports and use Webpack to join it together in one file, include the file in the HTML. Develop in multiple files, serve in one.
|
# ? Sep 16, 2015 04:38 |
|
piratepilates posted:No no, use ES6 style imports and use Webpack to join it together in one file, include the file in the HTML. Develop in multiple files, serve in one. This is good advice and will serve people well. (Though you reach a point where having the script out-of-line can be better.)
|
# ? Sep 16, 2015 04:39 |
|
piratepilates posted:No no, use ES6 style imports and use Webpack to join it together in one file, include the file in the HTML. Develop in multiple files, serve in one. I guess I'm confused, I looked up firefox's es6 support and it explicitly says it does not support module import/export yet. What am I missing (bear in mind I am dense). Edit, wait, does webpack just turn all of the javascript into a single file? Reading its tutorial has left me more confused than not. Knifegrab fucked around with this message at 04:45 on Sep 16, 2015 |
# ? Sep 16, 2015 04:43 |
|
Knifegrab posted:I guess I'm confused, I looked up firefox's es6 support and it explicitly says it does not support module import/export yet. What am I missing (bear in mind I am dense). Webpack reads the ES6 imports and creates a new file that has all of the files that were imported in one, you serve up only the new (one single) file, while developing in the other files. All browsers see are the one file, all you see is one built file and many source files. Your other option is to use some kind of weird runtime mess like AMD but this is 2015 please don't do that.
|
# ? Sep 16, 2015 04:45 |
|
piratepilates posted:Webpack reads the ES6 imports and creates a new file that has all of the files that were imported in one, you serve up only the new (one single) file, while developing in the other files. All browsers see are the one file, all you see is one built file and many source files. So if I do this in myLib.js code:
code:
|
# ? Sep 16, 2015 04:49 |
|
Knifegrab posted:So if I do this in myLib.js Yes. Here is the whole compiled file for that if you're curious: code:
piratepilates fucked around with this message at 04:53 on Sep 16, 2015 |
# ? Sep 16, 2015 04:50 |
|
piratepilates posted:Webpack reads the ES6 imports and creates a new file that has all of the files that were imported in one, you serve up only the new (one single) file, while developing in the other files. All browsers see are the one file, all you see is one built file and many source files. You can use Browserify to do the same thing, but either way, you have to set up a bundler to interpret the require/import statements. Also, TypeScript will allow you to use the ES6 import syntax, and will change them to var x = require("sdfdsf"); if targeting ES5.
|
# ? Sep 16, 2015 05:00 |
|
piratepilates posted:Yes. Can I do nested requires? I mean to say, can I have my main file require fileA, and then can I have fileA require fileB? Would that work or is it only capable of doing it one level deep?
|
# ? Sep 16, 2015 05:07 |
|
Knifegrab posted:Can I do nested requires? I mean to say, can I have my main file require fileA, and then can I have fileA require fileB? Would that work or is it only capable of doing it one level deep? Yes, it also probably deals with cyclic requires and the like. It's also very easy to test out, just install it through npm and do 'webpack <mainfile> <outputfile>'.
|
# ? Sep 16, 2015 05:13 |
|
piratepilates posted:Yes, it also probably deals with cyclic requires and the like. Yeah a minute after asking ths dumb question I had it working. Thanks a ton this is going to make my life much much better!
|
# ? Sep 16, 2015 05:18 |
|
Knifegrab posted:Yeah a minute after asking ths dumb question I had it working. Thanks a ton this is going to make my life much much better! Now if you get bored you can also go hog wild and start using require's for everything -- CSS, HTML, images, anything you can get a loader for you can write a require for and have Webpack fill it out.
|
# ? Sep 16, 2015 05:21 |
|
piratepilates posted:Now if you get bored you can also go hog wild and start using require's for everything -- CSS, HTML, images, anything you can get a loader for you can write a require for and have Webpack fill it out. I'll look into it, I'm ridiculously ecstatic to have bite sized files right now, as its my preferred organization method (I'm anal about code organization and structuring and modularization). edit: THANKS! <3
|
# ? Sep 16, 2015 06:21 |
|
Skandranon posted:You can use Browserify to do the same thing Get used to this when developing Javascript.
|
# ? Sep 16, 2015 11:46 |
|
i just use multiple .js files and <script> tags because i hate build processes lmao
|
# ? Sep 16, 2015 16:08 |
|
Suspicious Dish posted:i just use multiple .js files and <script> tags because i hate build processes lmao If you enable HTTP2 or SPDY on your server and your visitors are using modern browsers then this isn't even bad, it should (theoretically) work as well or better than concatenating files. I still like Webpack for the non-JS requires and all the nice loaders still.
|
# ? Sep 16, 2015 16:19 |
|
piratepilates posted:If you enable HTTP2 or SPDY on your server and your visitors are using modern browsers then this isn't even bad, it should (theoretically) work as well or better than concatenating files. I like webpack for those things (ES6, import, etc.) but the best thing is hot reloading.
|
# ? Sep 16, 2015 19:33 |
|
Lumpy posted:I like webpack for those things (ES6, import, etc.) but the best thing is hot reloading. Can you expand on hot reloading, and pretend I am a complete moron (I am).
|
# ? Sep 16, 2015 19:44 |
|
Knifegrab posted:Can you expand on hot reloading, and pretend I am a complete moron (I am). Hot reloading will watch for changes to your JS files and automatically inject the new script into your browser page on the fly. No reload is necessary and you see changes immediately. It's pretty spectacular, but took me some effort to get it working the first couple of times.
|
# ? Sep 16, 2015 21:46 |
|
|
# ? Jun 1, 2024 05:48 |
|
necrotic posted:Hot reloading will watch for changes to your JS files and automatically inject the new script into your browser page on the fly. No reload is necessary and you see changes immediately. It's pretty spectacular, but took me some effort to get it working the first couple of times. Ahhh gotcha, well for now I don't mind hitting up and then enter everytime I wanna test my new code, and I'm afraid of messing even THAT up!
|
# ? Sep 16, 2015 23:52 |