|
Munkeymon posted:That would use just as much memory which was what everyone seems to be concerned about. I mistakenly assumed trie was just a typo of tree, as in binary search tree, which would take the same amount of memory (or more) if I'm not mistaken. I learned two new data structures today!
|
# ? Aug 18, 2011 16:50 |
|
|
# ? May 16, 2024 12:18 |
|
ynohtna posted:There may be better methods but what I've done before for this in JS is build small iterator objects that allow next/previous node traversal as well as up & down by retaining the current node's path to root. Thanks, what I ended up doing was using this as the prototype for all the nodes, along with a custom iterator designed to filter on the isNode property. Works surprisingly well for being a dirty hack. code:
|
# ? Aug 18, 2011 20:22 |
|
still trying to get my head around JavaScript's object model... so in general if I'm writing a "singleton" sort of object that doesn't ever need to be instantiated with 'new' I'm fine adding methods directly to the object/function right? like this,code:
code:
|
# ? Sep 6, 2011 21:22 |
|
My dad has been hinting very strongly that he wants a book on javascript, are there any really awesome resources?
|
# ? Sep 14, 2011 05:21 |
|
Every object in Javascript can be considered a singleton. The 'new' keyword just returns the object's prototype and isn't actually as useful as it's made out to be. All you need is:code:
quote:My dad has been hinting very strongly that he wants a book on javascript, are there any really awesome resources? The Crockford book is all you need if you have any experience with any other language.
|
# ? Sep 14, 2011 05:37 |
|
Deus Rex posted:still trying to get my head around JavaScript's object model... so in general if I'm writing a "singleton" sort of object that doesn't ever need to be instantiated with 'new' I'm fine adding methods directly to the object/function right? like this, If you are going to create lots and lots and lots of instances of an object, you use your second example along with new to create them. This has the advantage of not creating instances of member functions for every instance of your object, as it will look to the (single) prototype for it. If you are creating a singelton, you can either use object literal notation as in NotShadowStar's example, or you can use what is generally called the "Module Pattern" to invoke a self-calling function. The advantage of that is you can have private variables and methods, which you can't do in object literal fashion: code:
|
# ? Sep 14, 2011 12:34 |
|
Do you guys feel like some kind of unit testing suite like QUnit is important for javascript developers to work into their repertoire? I want to be a stronger JS developer, and learning that is something I'm considering.
|
# ? Sep 22, 2011 18:58 |
|
This isn't strictly a Javascript question, but is there a way to find the margin of an element if it has not been assigned from javascript? Example document and java script: code:
code:
That Javascript snippet is an empty string. You can find the height and border widths by using offsetHeight and clientHeight, but is there any such trick for margin? This is something that would need to work (only) in FF 3.6, if at all. (maintaining poo poo code)
|
# ? Sep 28, 2011 15:53 |
|
Wheany posted:This isn't strictly a Javascript question, but is there a way to find the margin of an element if it has not been assigned from javascript?
|
# ? Sep 28, 2011 16:43 |
|
The Merkinman posted:You should be able to do that in jQuery with.css() Yeah, there is no jQuery. We have MooTools, I could look into that.
|
# ? Sep 28, 2011 18:47 |
|
getComputedStyle.
|
# ? Sep 28, 2011 23:43 |
|
OddObserver posted:getComputedStyle. Yes, thank you.
|
# ? Sep 29, 2011 05:35 |
|
I'm looking for a JS library that lets me visualize network/force/etc. diagrams, but lets me use HTML objects (e.g., a div) as nodes. I've seen a few different graphing libraries that do network/force/etc. diagrams, but they render their nodes in their native type, like SVG or <canvas> graphics. I need to use intractable <div>s as the node.
|
# ? Sep 30, 2011 22:57 |
|
Magicmat posted:I'm looking for a JS library that lets me visualize network/force/etc. diagrams, but lets me use HTML objects (e.g., a div) as nodes. I haven't used it myself, but the approach taken by d3.js might be the thing you're looking for. http://mbostock.github.com/d3/ex/force.html
|
# ? Oct 2, 2011 06:21 |
|
trinary posted:I haven't used it myself, but the approach taken by d3.js might be the thing you're looking for.
|
# ? Oct 2, 2011 06:38 |
|
I think I misunderstood what you wanted when giving the recommendation, but d3 might not end up being entirely wrong for your purposes. The overview page might provide a better description. http://mbostock.github.com/d3/ It's basically a binding system between DOM elements and arbitrary data. SVG is a convenient way to turn that into a visualization, but instead of binding to SVG elements you can bind to whatever DOM elements you want. In theory, anyway. I don't have any practical experience with it.
|
# ? Oct 2, 2011 06:47 |
|
I have a problem I can't figure out in backbone.js and underscore.js. code:
Calling app.addAll() or just running app.stuffs.each(app.addOne) works as well from the chrome console, however some reason not in a script. So why doesn't the this.stuffs.models work? I have a very weak foundation in Javascript, so I could be missing something very simple.
|
# ? Oct 6, 2011 06:36 |
|
I've checked a few pages back, but couldn't find this issue or similar. I have a page listing multiple contacts at various companies. They're organised in a list, with each company rendered as a 'block' that can be opened or closed with a link through a simple javascript function. The example is here: example page. In the spirit of writing unobtrusive javascript, I have a script adding all the ids and hooks that are needed for the toggle function to work. Nearly all of it works perfectly, except for one rather important thing: the actual toggle link itself. I'm trying to add an onmousedown event to the link which calls the toggle function and passes the variable for that particular company, but for some reason every link ends up passing the variable for the last company in the list instead. Since the ids are getting added properly for each company, this line must be at fault, but I don't know for the life of me why. It does work, except not for the right company. It's probably because I'm not very good with javascript code:
Leshy fucked around with this message at 21:55 on Oct 10, 2011 |
# ? Oct 10, 2011 21:21 |
|
Leshy posted:
All of the contactId's refer to the same variable. At the end of the loop it's "company4", so every toggleContactLayer call gets the same value. You could do this: code:
That last line with the parentheses just calls the outer function immediately so that "myId" gets assigned and the inner function created.
|
# ? Oct 10, 2011 21:57 |
|
PresidentCamacho fucked around with this message at 02:03 on Feb 27, 2012 |
# ? Oct 11, 2011 00:41 |
|
Leshy posted:I've checked a few pages back, but couldn't find this issue or similar. HTML5's summary/details markup plus the polyfill would be my solution.
|
# ? Oct 11, 2011 01:40 |
|
Wheany posted:All of the contactId's refer to the same variable. At the end of the loop it's "company4", so every toggleContactLayer call gets the same value. On each iteration of the loop, the variable has a different value, so I expected that much like in setting the id of an element, setting the onmousedown event would assign the then current value of the variable. Eg: code:
code:
MonkeyMaker posted:HTML5's summary/details markup plus the polyfill would be my solution.
|
# ? Oct 11, 2011 06:59 |
|
Leshy posted:I figured something like this must be happening, I'm just not sure why. Inner functions have access to outer function's scope. Your code has 2+n scopes: Global scope variables: var contact, contactID, i, contactCompany, contactHeader, contactToggle, contactContent; Other scopes: function toggleContactLayer, contact.length * anonymous function (the ones you assign to contactToggle[0].onmousedown) Every one of those anonymous functions has their own scope, but can also access outer (global) variables. So each anonymous function refers to the exact same (global) contactID. When you assign the onmousedown, the Javascript engine just thinks "that sure is a function". Only when the event fires does the engine care about what happens inside the function. In your case it is simply toggleContactLayer( (global) contactID). There are several anonymous functions, but only one contactId (the global one). My example: code:
Well, the first set of parentheses actually contains a function definition, so you're defining an anonymous function and then immediately invoking it. When you invoke the function, you create a new scope, with the variable "myId". Each myId sits inside its own anonymous scope and gets assigned the current value of the global contactID. Then, from inside this anonymous scope, you return a new anonymous inner function . This anonymous inner function gets assigned to onmousedown, and once again the Javascript engine just thinks "that sure is a function". Finally, when the event fires, the anonymous inner function gets run. Again, it's simply toggleContactLayer( (the outer function's) myId). There are several outer functions, so there are several myId's quote:On each iteration of the loop, the variable has a different value, so I expected that much like in setting the id of an element, setting the onmousedown event would assign the then current value of the variable. No, the code inside function(){} is, well, code. It is evaluated when the function is called. You could have assigned contactToggle[0].onmousedown = function(){toggleContactLayer(thisvariableDoesNotEvenExist)}, and you would only get an error on the mousedown event. quote:Your solution works perfectly, however, so thanks a lot! I'm still learning this whole javascript thing as I go along, and I couldn't really find a solution for this. Another thing learned! Well, I only started to appreciate Javascript's (good) weirdness in the last 6 months or so.
|
# ? Oct 11, 2011 12:59 |
|
Well, that sure is a thorough explanation! indeed. I do get why it's happening now, though, so once again thanks. Knowing why something is going wrong is usually more instructional than knowing what the fix is
|
# ? Oct 11, 2011 18:49 |
|
I have been playing with Javascript a little bit but coming from C/Ruby, everything about the language is strange to me. I'm making a change to an existing web page where you can choose an object by it's color, red/yellow/blue/orange/green/other. It's using a JQuery accordion view and we're going to get rid of that. Instead there needs to be an alphabetical categorization/search. There's already a search box where you can enter a couple letters of the objects name and that works fine. What I was going to do is add each letter at the top of the page, and then if you clicked on 'F' it would only show the objects that had names starting with 'F'. I'll probably make a copy of the existing function so that it takes the letter and returns results that start with 'F' instead of contain 'F' (which is how it works now). I'm stuck on what kind of HTML tag I should make each letter so that when they are clicked, I can pass that letter to the search function. Right now the code that makes the page is something like: from A to Z do letter write <a href='#' id='letter_link'>letter</a> end Should I add another property like letter='%letter&' to the <a> tag? Could I think grab it via DOM? Should I put the letter in the 'letter_link' id (letter_link_a, letter_link_b) and parse it out? I'm just lost and need to buy a JQuery book this week.
|
# ? Oct 12, 2011 02:15 |
|
Bob Morales posted:from A to Z do letter Are you literally using id="letter_link" for every letter? Because id's are supposed to be unique. I'd use class="letter_link", and then something like id = "letter_a", "letter_b" and so on. In the onclick handler, your letter would be this.id.charAt(7).
|
# ? Oct 12, 2011 06:52 |
|
This is what data attributes are forcode:
|
# ? Oct 12, 2011 13:29 |
|
Wheany posted:Are you literally using id="letter_link" for every letter? Because id's are supposed to be unique. I'd use class="letter_link", and then something like id = "letter_a", "letter_b" and so on. I ended up doing this, thanks.
|
# ? Oct 12, 2011 14:58 |
|
I'm curious, is there something wrong with just grabbing the value inside the <a> tag and using that? Why add the extra properties?
|
# ? Oct 14, 2011 17:47 |
|
Keevon posted:I'm curious, is there something wrong with just grabbing the value inside the <a> tag and using that? Why add the extra properties? That would work at least now and in this case. But the first thing that I could think of was if you later change the structure of the page like so: <a href="#"><img src="a.gif"></a>, you'd have to change your javascript as well.
|
# ? Oct 14, 2011 19:36 |
|
Wheany posted:That would work at least now and in this case. But the first thing that I could think of was if you later change the structure of the page like so: <a href="#"><img src="a.gif"></a>, you'd have to change your javascript as well. Thanks, I was thinking something about how if you had changed the view portion of it your data would break, but in this case I couldn't think of how you might replace the letter since it's a simple case. I certainly understand if it's a much more complex view structure. This makes sense to me.
|
# ? Oct 14, 2011 22:29 |
Anyone have any ideas as to why this code isn't working? http://jsfiddle.net/mGdcm/ HTML- code:
code:
az jan jananam fucked around with this message at 02:11 on Oct 18, 2011 |
|
# ? Oct 18, 2011 01:32 |
|
Because css("opacity") returns a string, as a 5-second test using typeof would show. Also, you really should be beware of equality-testing floating point numbers exactly --- not that it's the issue here. Edit: woops, didn't see the link --- so you're using mootools and not jQuery? Under mootools $('#toggleButton') evaluates to null. OddObserver fucked around with this message at 02:36 on Oct 18, 2011 |
# ? Oct 18, 2011 02:31 |
|
This works: http://jsfiddle.net/mGdcm/23/ 1. I included the jQuery framework. 2. When jQuery completes a fadeOut it sets the target element's display property to "none" and resets it's opacity to 100. Thus, testing for display none will correctly choose the fadeIn path. (Personally, I'd add a marker class to the element after it's completed the fade out and check for that.) 3. Check your browser's JavaScript error console: "TypeError: 'null' is not an object (evaluating '$('#toggleButton').click')" ynohtna fucked around with this message at 02:42 on Oct 18, 2011 |
# ? Oct 18, 2011 02:33 |
|
I want to play with Javascript and gain a better understanding of how the language works by writing a few object prototypes and playing with language features like closures. Is there anything like irb (ruby) for JavaScript, or would my best choice for this kind of exploration be my web developer tools console?
|
# ? Oct 18, 2011 02:40 |
|
smug forum rear end in a top hat posted:I want to play with Javascript and gain a better understanding of how the language works by writing a few object prototypes and playing with language features like closures. Is there anything like irb (ruby) for JavaScript, or would my best choice for this kind of exploration be my web developer tools console? http://jsfiddle.net in conjunction with your browser's web developer tools is a good start for interactive exploration.
|
# ? Oct 18, 2011 02:45 |
|
az jan jananam posted:Anyone have any ideas as to why this code isn't working? http://jsfiddle.net/mGdcm/ Because opacity doesn't have anything to do with the end state of the element after fadeOut or fadeIn, and as was pointed out, even if it did, you are testing for it wrong. Plus, look at the example I posted in the jQuery thread when you asked your very similar question there about using the "toggle" variants of effects to avoid writing extra code.
|
# ? Oct 18, 2011 12:07 |
|
ynohtna posted:http://jsfiddle.net in conjunction with your browser's web developer tools is a good start for interactive exploration. If you just want to poke at the JavaScript language without messing with HTML, there's also http://jsconsole.com/ and http://www.jconsole.com/
|
# ? Oct 18, 2011 15:15 |
Lumpy posted:Because opacity doesn't have anything to do with the end state of the element after fadeOut or fadeIn, and as was pointed out, even if it did, you are testing for it wrong. Plus, look at the example I posted in the jQuery thread when you asked your very similar question there about using the "toggle" variants of effects to avoid writing extra code. Thanks!
|
|
# ? Oct 18, 2011 18:11 |
|
|
# ? May 16, 2024 12:18 |
|
I'm trying to make checkboxes that can change the border of a table and it works perfectly in Chrome, Firefox and newer IEs, but in IE 6 and 7 it's giving me beef.code:
EDIT: element is a JQuery element. Probably a little relevant. Boz0r fucked around with this message at 15:47 on Oct 19, 2011 |
# ? Oct 19, 2011 15:01 |