|
Mogomra posted:Here's a stupid question that doesn't deserve its own thread. What's the deal with "new," or rather not using it? What's the logic behind defaulting "this" to the window, or whatever the global object is? Why not default to "this" from the scope that the function was called? It's stupid, and there's no reason not to use "new" when it's needed, I just don't get why JavaScript does what it does when you leave it out. Constructors are just regular functions. Understanding that fact will answer all your questions.
|
# ? Oct 22, 2013 03:04 |
|
|
# ? May 28, 2024 14:29 |
|
Mogomra posted:Here's a stupid question that doesn't deserve its own thread. What's the deal with "new," or rather not using it? What's the logic behind defaulting "this" to the window, or whatever the global object is? Why not default to "this" from the scope that the function was called? It's stupid, and there's no reason not to use "new" when it's needed, I just don't get why JavaScript does what it does when you leave it out. It defaults to undefined in strict mode. Dynamic scope is a trap and hard to optimize, though, so no. ES6 will have the lexical this you're used to with arrow functions.
|
# ? Oct 22, 2013 03:23 |
|
I get that, I really do. I just don't understand why these to things end up being the same.code:
I get that you should use Function.call() or . apply() in that case, but why does it always default to the global object if you don't? I guess the more I think about it, each function is part of the window object, but then what happens if I do something like: code:
Would 'this' also end up being window if you don't use new?
|
# ? Oct 22, 2013 03:45 |
|
Mogomra posted:Would 'this' also end up being window if you don't use new? JS does not have lexical closures, it's based on the syntax of how you called the object. If you do o.f();, this is o. If you do var f = o.f; f();, this is the global object. If you do new f(); or new o.f();, this is an object constructed from the constructor's prototype property. Imagine var this = Object.create(Thing.prototype); at the top of your constructor function.
|
# ? Oct 22, 2013 03:57 |
|
JOHN SKELETON posted:The company I work for uses this pattern: Your company are a bunch of monsters for capitalizing SuperClass but not Subclass. But seriously, this doesn't work. This doesn't allow SubClass to inherit any properties of SuperClass. JavaScript code:
|
# ? Oct 22, 2013 06:36 |
|
Suspicious Dish posted:JS does not have lexical closures, it's based on the syntax of how you called the object. This answers the question I was asking pretty clearly. Thanks!
|
# ? Oct 22, 2013 15:12 |
|
Strong Sauce posted:Your company are a bunch of monsters for capitalizing SuperClass but not Subclass. JavaScript code:
TURTLE SLUT fucked around with this message at 03:07 on Oct 23, 2013 |
# ? Oct 23, 2013 03:03 |
|
The problem with that approach is it creates a new copy of each function for each instance of the class, which massively increases memory usage. From a design perspective I'm a fan since it means you get private variables, but it just doesn't scale very well.
|
# ? Oct 23, 2013 03:24 |
|
Plorkyeran posted:The problem with that approach is it creates a new copy of each function for each instance of the class, which massively increases memory usage. From a design perspective I'm a fan since it means you get private variables, but it just doesn't scale very well. This isn't quite true. Functions aren't duplicated; bodies are compiled once and then closed over at runtime. All that means is that you have a new closure, of which the scope chain is shared between all the other methods in the body, which isn't too big an issue with memory consumption. The "hidden class"-type analysis most engines do nowadays also work to identify this pattern.
|
# ? Oct 23, 2013 04:20 |
|
The big thing is that now each object has a pointer to every single method (instead of what is effectively a vtable pointer to the prototype), which is quite a lot of bloat for classes with a lot of methods and not many data fields.
|
# ? Oct 23, 2013 05:14 |
|
Suspicious Dish posted:This isn't quite true. Functions aren't duplicated; bodies are compiled once and then closed over at runtime. All that means is that you have a new closure, of which the scope chain is shared between all the other methods in the body, which isn't too big an issue with memory consumption. The "hidden class"-type analysis most engines do nowadays also work to identify this pattern. If you do what JOHN SKELETON is doing, you still need to create objects and allocate space for them. But now you are also allocating the space for functions instead of just having the objects reference the prototype. Hidden classes AFAIK just speed property lookups by using offsets to the current object being created rather than using a lookup table. So what I mean is, if you have JavaScript code:
|
# ? Oct 23, 2013 07:36 |
|
I'm having trouble with the simplest thing: Two divs, hidden at first. Two radio buttons: Option 1, Option 2. Clicking option 1 shows div 1 (and hides div 2 if it's open), and clicking option 2 shows div 2 (and hides div 1). I've googled the poo poo out of javascript examples but am having a hell of a time making anything work how I want. This is what I want, but I'm working inside someone's workpress contact form, and I can't figure out if I have enough control to do this. http://jsfiddle.net/HREqE/ It's the kind of thing where I've got a dropdown and I choose "radio" - it generates this, and I'm adding the data-rel at the end. Soon as the data-rel goes in there, the saved page won't show the radios. This is why I don't enjoy working on wordpress sites. jackpot fucked around with this message at 22:54 on Oct 25, 2013 |
# ? Oct 25, 2013 22:21 |
|
You don't need to save the page in order to look at the HTML as it's being rendered - just press F12 in any non-poo poo browser.
|
# ? Oct 26, 2013 00:27 |
|
I'm writing a module using the following pattern:JavaScript code:
JavaScript code:
|
# ? Oct 26, 2013 21:08 |
|
No, you really cannot poke into closures like this. In my idealist view, if your unit tests needs to poke into internal state, it shouldn't be internal. Maybe public isn't appropriate, but perhaps it can be namespace-private like Python has, by prefixing "secret" data with an underscore, by convention?
|
# ? Oct 26, 2013 22:07 |
|
That's a good idea. I'm pretty sure the answer to this is yes but are closures the only way to control visibility in js?
|
# ? Oct 27, 2013 05:11 |
|
Until private name objects arrive in engines, yes.
|
# ? Oct 27, 2013 05:59 |
|
I realize this maybe isn't the best thread, but I wanted to start a project to learn javascript. I really enjoy cryptography, as I really loved number theory in my Discrete Math classes. So, I'm trying to figure out a neat project with js to maybe be some sort of RSA encrytor or something, but I'm kinda at a lost for ideas.
|
# ? Oct 30, 2013 23:36 |
|
Have you written an encryptor in another language? Try doing a line-by-line straightforward port of it to JS. Next, read a book or two on JS and see if you can't go back and do it again with a better understanding of scope and prototypes and perhaps a library like Parallel.js
|
# ? Oct 31, 2013 13:52 |
|
I am creating a visualization using google maps api v3. One of the big components is to be able to render polygons which 'outline' regions such as zip code, state.... This needs to be done dynamically so kml files or fusion tables wont work. So I have data for each type of polygon I want to render, this data was initially in Json format. My application can consume this format and render polygons. I found that rendering ~3000 polygons took about 8 seconds. Not terrible but I thought I could make it quicker. To make it quicker I decided to "prebuild" the google maps api syntax necessary to render each polygon, this would be stored in a database. Once its stored in a database I am using restful web services to bring back these values. https://www.applicatin.com/giveZip&val=22031 Now my problem is storing these google polygons in the database with a few hundred points is a difficult thing to do. It seems like some of the syntax is unnecessary such as repeating 'new google.maps.LatLng' for every single point. How can I simplify these polygons syntax? note I am a pretty big novice when it comes to javascript
|
# ? Oct 31, 2013 15:52 |
|
I've built KML files dynamically. It's not that hard as long as you have good XML support in your server-side language. I made the maps in Google Earth, split them up programmatically, and recombined them based upon user selections.
|
# ? Oct 31, 2013 17:08 |
|
Is that a faster approach than what I am doing?
|
# ? Oct 31, 2013 18:15 |
|
DholmbladRU posted:Is that a faster approach than what I am doing? It seems to be pretty speedy, considering that Google downloads your KML and translates it into a map overlay. But I haven't rendered 3000 polygons that way, I was doing lines and placemarks.
|
# ? Oct 31, 2013 18:56 |
|
DholmbladRU posted:To make it quicker I decided to "prebuild" the google maps api syntax necessary to render each polygon, this would be stored in a database. Why did you choose to do this? Did you benchmark and determine that reading in your JSON was the bottleneck in your code?
|
# ? Nov 1, 2013 03:17 |
|
AngularJS + JSON question: I'm trying to use the orderBy filter on my ngRepeat. The structure of my JSON looks like thispre:{ "properties": { "name": "Risk", "caption": "Risk", "readOnly": 0 }, "Risk": [ { "id": 0, "RiskOutput.Address": { "value": "490 Wythe Avenue, Bordelonville, South Dakota, 9771", "caption": "Address", "maxLength": 100, "required": false, "applicable": true, "readOnly": true, "annotations": true }, "RiskOutput.Description": { "value": "Location #65", "caption": "Location", "maxLength": 30, "required": false, "applicable": false, "readOnly": true, "annotations": true }, "RiskOutput.ID": { "value": "e487ac57-8de8-4794-b562-ff5556eda79f", "caption": "ID", "maxLength": 50, "required": false, "applicable": false, "readOnly": false, "annotations": true } }, { "id": 1, "RiskOutput.Address": { "value": "136 Lawrence Avenue, Statenville, North Carolina, 7118", "caption": "Address", "maxLength": 100, "required": true, "applicable": true, "readOnly": false, "annotations": true }, "RiskOutput.Description": { "value": "Location #37", "caption": "Location", "maxLength": 30, "required": false, "applicable": false, "readOnly": false, "annotations": true }, "RiskOutput.ID": { "value": "4d582882-d462-45c3-b049-5e8a052f46d2", "caption": "ID", "maxLength": 50, "required": true, "applicable": true, "readOnly": true, "annotations": true } }, etc... pre:<tr ng-repeat="risk in pageData.Risk | orderBy:'[RiskOutput.Description].value'"> <td>{{risk["RiskOutput.ID"].value}}</td> <td>{{risk["RiskOutput.Description"].value}}</td> <td>{{risk["RiskOutput.Address"].value}}</td> <td class="btn-toolbar"><button class="btn btn-xs btn-primary">Edit</button> <button class="btn btn-xs btn-danger">Delete</button></td> </tr>
|
# ? Nov 3, 2013 04:42 |
|
In node.js\express(but probably a javascript question): Why is it that if I use code:
code:
|
# ? Nov 4, 2013 00:38 |
|
bobua posted:In node.js\express(but probably a javascript question): When you do "function(req, rest){ login.login_handler(req, res, dynode);)" you're creating a function that takes two parameters and returns whatever value "login.login_handler(req, res, dynode)" returns, when you do "app.post('/login_form_action', login.login_handler(req, res, dynode));" you're giving the second parameter as the return value of a function that is evaluated immediately (more or less), so it looks up req and res since it expects those to be variables in scope at that time and gets confused because there isn't anything there, because they don't exist yet. Look up closures because I'm pretty sure that's what this is (I'm poo poo at remembering the proper terms for what's going on). The best way to think about it is that in your first example you're passing "app.post(...)" an object that represents a function and is called later when it's needed (and when req and res exist) while in the second one you're calling a function outright and it expects the variables it needs to exist right there and then.
|
# ? Nov 4, 2013 01:09 |
|
bobua posted:In node.js\express(but probably a javascript question): It's simple. In the first one you are passing in a function, the second one you are passing in a functional call that returns something (which could be valid if your function returns a function). req and res don't exist in scope in the 2nd example, thus they have undefined values. What you should be passing is the function, login.login_handler, without any parameters. Keep in mind, the callbacks in express actually have a signature of: function(req, res, next, error), so if dynode is not passing down the express handler via next(), then you cannot actually pass in 'login.login_handler' as a function because that will actually hang your application.
|
# ? Nov 4, 2013 01:20 |
|
I'm still confused. I thought app.post provided the req\res variable. If I'm 'creating' those variables with function(req,res) {... Then what is populating them with data? When I use them in the login handler function, they contain the info I'm looking for(req.body.email for example).
|
# ? Nov 4, 2013 01:38 |
|
bobua posted:I'm still confused. I thought app.post provided the req\res variable. If I'm 'creating' those variables with function(req,res) {... Then what is populating them with data? When I use them in the login handler function, they contain the info I'm looking for(req.body.email for example). In your first example you're creating a function that calls that login function and giving the new function to app.post to use later, later on app.post will call that new function with a res and req in scope. In your second example you're calling the login function as the second parameter to app.post, the javascript interpreter will look at that and go "oh they want to call a function, let's evaluate that function now and use the return value as the second parameter to app.post", but calling it now doesn't make sense and res and req don't exist in it's scope (since they exist only inside what app.post calls) so it won't work. What you want to be doing there is giving app.post the function you want to call when it's time for app.post to call a function, you have to have some construct in the language that tells the interpreter that you're passing the actual function to app.post and not just calling that function, the way to do that is to use function(){} to create a new function and have it contain the logic you want to run later when app.post uses your callback function. What you want to do is pass app.post a callback function -- a function that takes the right number of parameters and can be used by app.post later on for it's own nefarious purposes, the names res and req don't have any relevance either actually, you can name the parameters to that new function poop and fart and it'll still work, the new function is just a function beng returned that takes two parameters.
|
# ? Nov 4, 2013 01:59 |
|
bobua posted:I'm still confused. I thought app.post provided the req\res variable. If I'm 'creating' those variables with function(req,res) {... Then what is populating them with data? When I use them in the login handler function, they contain the info I'm looking for(req.body.email for example). The first is variable scoping. The second is not knowing that javascript can pass a function as a parameter because in javascript they are first class citizens. In your first example, you are passing a string and then a function as the two parameters, the second example you are passing a string and a return value of whatever login.login_handler is suppose to return. Since in the second example you are executing a function call, it needs to pass values into the login.login_handler to get a return result. But when you do that, javascript needs to resolve req and res, which do not exist in the current scope. So they will pass undefined to the login.login_handler function. You need to ignore the fact that in the first example the function name passes two parameters called req and res. Because that is irrelevant. You instead need to look at it as a function that passes two parameters. Callbacks, closures, and functions as first-class citizens are important concepts you need to learn in javascript. So if they don't seem clear, you may want to look them up in a book or online to understand them further.
|
# ? Nov 4, 2013 02:17 |
|
Think my minds on the right track now, than you gentlemen. Just dove into Javascript: The good parts, which may be a little dated since it came out in 2008 but between that and google I think I'll be okay, unless there is a book you all recommend.
|
# ? Nov 4, 2013 02:36 |
|
bobua posted:Think my minds on the right track now, than you gentlemen. Just dove into java script: The good parts, which may be a little dated since it came out in 2008 but between that and google I think I'll be okay, unless there is a book you all recommend. I really hated The Good Parts. I'd suggest reading the MDN documentation and buying Effective: Javascript by David Herman.
|
# ? Nov 4, 2013 14:22 |
|
excidium posted:AngularJS + JSON question: I'm trying to use the orderBy filter on my ngRepeat. The structure of my JSON looks like this Your object keys are really janky for working with angulars filters, so this may not work, but try escaping the inner key: "orderBy:'[\'RiskOutput.Description\'].value'" If that fails I think you should remove the dots in your keys so you can access the attributes without the square bracket notation. ie riskOutputDescription or something similar. That way you could use order by: riskOutputDescription.value Alternatively you can create a getter function in scope that will take your object and return the description value for sorting. Give that function to orderBy.
|
# ? Nov 4, 2013 14:36 |
|
rsjr posted:I really hated The Good Parts. I'd suggest reading the MDN documentation and buying Effective: Javascript by David Herman. Counterpoint: I really loved The Good Parts. I'd suggest reading it many times, as well as reading MDN docs.
|
# ? Nov 4, 2013 14:38 |
|
So I'm gonna begin using Knockout.js for a small website project. It's not going to be much more than a button to click and some text boxes that appear. Do I need to learn anything more complicated like AJAX or whatever, or is just going through the KO documentation/tutorial enough?
|
# ? Nov 4, 2013 21:34 |
|
Pollyanna posted:Do I need to learn anything more complicated like AJAX or whatever, or is just going through the KO documentation/tutorial enough? AJAX is only for talking to a server without reloading the page. If you don't have a need for that, then you shouldn't have to learn it.
|
# ? Nov 4, 2013 21:40 |
edit: whoops I should probably put this in the jQuery thread
fletcher fucked around with this message at 23:59 on Nov 4, 2013 |
|
# ? Nov 4, 2013 22:08 |
|
Bognar posted:AJAX is only for talking to a server without reloading the page. If you don't have a need for that, then you shouldn't have to learn it. Ahh, right. My page isn't going to be handling a whole lot of data or keeping a database or anything, so I don't foresee the need to keep querying the server to load data. That reminds me, I'm wondering whether Knockout or jQuery is the best way to go about my project. Knockout appears to be good for forms and emails and stuff, which isn't really what I'm going to do - it's probably just going to be a counter, basic animation and some graphics. Is Knockout really the best tool for this, or should I use something that's more appropriate?
|
# ? Nov 4, 2013 22:11 |
|
|
# ? May 28, 2024 14:29 |
|
Speaking of AJAX(which I've never used)... Let's say you wanted a little message box on a page, and you wanted the server to be able to send messages to that box at any time, not just when the user clicks a button or something on the page. Would AJAX be the right avenue for that sort of thing? I went through the w3c's AJAX tutorial and it seemed close, but all of the examples were more about the page setting up a request and waiting for a response.
|
# ? Nov 4, 2013 23:05 |