Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
piratepilates
Mar 28, 2004

So I will learn to live with it. Because I can live with it. I can live with it.



Wheany posted:

Sure, but it doesn't support poo poo like Array.from, requestAnimationFrame and MutationObserver, all of which I used in this script. The first two I could circumvent with Array.prototype.whatever.call() and setTimeout(,0), but the last bit I just had to comment out. :(

It's so loving dumb that Chrome says that "you might have been tricked into installing this", or whatever, but there is no "no I was not, I just wrote the drat thing myself" button. Well except maybe the developer mode, maybe.

e: The developer mode lets you open a directory that has the same structure as a Chrome extension. It does not enable you to just pick a .user.js file.

If it's just a user script and not an extension you just have to check developer mode and drag the .user.js file in to the extensions window. Then it will show up normally in developer tools under sources (I forget which sub tab of sources it will be under, probably just sources).

Adbot
ADBOT LOVES YOU

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Yeah, that worked

Video Nasty
Jun 17, 2003

I know it was asked on the last page, but http://www.regex101.com has saved my rear end hundreds of times when I need to be certain that I got my matching right.

waffle enthusiast
Nov 16, 2007



Anyone know of a good ical parsing library for Node? I'm using ical, but am getting bit hard by this bug, wherein the library parses the event in the system's local time (UTC), even though the event is listed in US/Mountain.

I can use moment-timezone and parse every call to event start/stop times in the correct timezone (yuck; this means passing a 'timezone' for every calendar I want to parse), but I'm hoping someone might know of a more elegant solution/better ical client library.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Dangerllama posted:

Anyone know of a good ical parsing library for Node? I'm using ical, but am getting bit hard by this bug, wherein the library parses the event in the system's local time (UTC), even though the event is listed in US/Mountain.

I can use moment-timezone and parse every call to event start/stop times in the correct timezone (yuck; this means passing a 'timezone' for every calendar I want to parse), but I'm hoping someone might know of a more elegant solution/better ical client library.

I think mozilla's ical.js is pretty complete: https://github.com/mozilla-comm/ical.js/

lunar detritus
May 6, 2009


How would you represent a table with N row headers and N col headers in a way amenable to render it with angular?

pre:
+-------+------------------------+-------------------+
|       |          2014          |        2015       |
|       +----+----+----+----+---------+----+----+----+
|       |  1 |  4 |  5 |  6 | 12 |  3 |  4 |  5 | 10 |
+---+------------------------------------------------+
|   |   |    |    |    |    |    |    |    |    |    |
| U |NY | 80 | 87 | 83 | 82 | 84 | 94 | 63 | 42 | 64 |
| S +------------------------------------------------+
| A |   |    |    |    |    |    |    |    |    |    |
|   |WA | 80 | 73 | 95 | 73 | 73 | 84 | 64 | 75 | 85 |
|   +------------------------------------------------+
|   |   |    |    |    |    |    |    |    |    |    |
|   |OR | 74 | 74 | 74 | 74 | 83 | 54 | 75 | 53 | 74 |
+-------+----+----+----+----+----+----+----+----+----+

Right now we're using different objects to represent the vertical and the horizontal headers but it doesn't really ensure the order of the cells.

necrotic
Aug 2, 2005
I owe my brother big time for this!
The only way to ensure order is using arrays, objects in Javascript are not ordered. For a 2D grid, use a 2-dimensional array where the first dimension is rows and the second is columns.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

necrotic posted:

The only way to ensure order is using arrays, objects in Javascript are not ordered. For a 2D grid, use a 2-dimensional array where the first dimension is rows and the second is columns.
This is true for the table data. You'll also want some kind of ordered lookup with row and column names.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

necrotic posted:

The only way to ensure order is using arrays, objects in Javascript are not ordered. For a 2D grid, use a 2-dimensional array where the first dimension is rows and the second is columns.

Objects and Arrays can both have numbered properties, which can be looped over with for. The only real difference between Arrays and Objects is that Arrays maintain a length property.

lunar detritus
May 6, 2009


In the end the boss solved it with a node tree.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Subjunctive posted:

Objects and Arrays can both have numbered properties, which can be looped over with for. The only real difference between Arrays and Objects is that Arrays maintain a length property.

http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf

quote:

4.3.3 Object
An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

So, sure, you can use numbers if you want. But the order is not guaranteed! You may experience behavior that makes it look like it is, but the spec doe not require it and browsers do behave differently.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

necrotic posted:

So, sure, you can use numbers if you want. But the order is not guaranteed! You may experience behavior that makes it look like it is, but the spec doe not require it and browsers do behave differently.

Arrays aren't ordered either, you just access their numeric properties in order by looping from 0 to length; you can do the same with an Object. See 15.4 of the ES5 spec, or the specification of things like Array.prototype.forEach which explicitly count from 0 to length. 12.6.4 explicitly says that the enumeration order is not specified, and that holds for all objects including Arrays.

I think you're confusing JSON's (non-)guarantees about the order that properties appear in serializations with some inherent property order for Arrays.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
That is true, but every engine special-cases for (var i in arr) because existing web applications depend on property iteration order of arrays.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Subjunctive posted:

Arrays aren't ordered either, you just access their numeric properties in order by looping from 0 to length; you can do the same with an Object. See 15.4 of the ES5 spec, or the specification of things like Array.prototype.forEach which explicitly count from 0 to length. 12.6.4 explicitly says that the enumeration order is not specified, and that holds for all objects including Arrays.

I think you're confusing JSON's (non-)guarantees about the order that properties appear in serializations with some inherent property order for Arrays.

Right, but if you care about order why would you use an Object with numeric keys instead of an Array (which gives you forEach that is ordered, whereas Object.keys().forEach would not be)? The only case I could see using an Object over an Array is if your starting index isn't 0 or something (since every index before N would "exist" as undefined).

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Suspicious Dish posted:

That is true, but every engine special-cases for (var i in arr) because existing web applications depend on property iteration order of arrays.

Actually, engines switched to numeric order because it fit into optimized storage setups. Previously, arrays were objects in how they stored properties, and shared Objects' enumeration order (which was was property addition order by de facto standard, pending ES4 de jure standardization, and on that topic there is a lot of remaining compatibility baggage today). Moving to dense vectors for (most) Array storage meant either tracking property addition order separately, or enduring some compatibility breakage by enumerating numeric properties in numeric order regardless of the order in which they were added. I broke a few sites with Firefox 3 when I did that, but we decided to roll with it.

necrotic posted:

Right, but if you care about order why would you use an Object with numeric keys instead of an Array (which gives you forEach that is ordered, whereas Object.keys().forEach would not be)? The only case I could see using an Object over an Array is if your starting index isn't 0 or something (since every index before N would "exist" as undefined).

My point was more that Arrays aren't standardized to be ordered, so "they're ordered" isn't a reason to use them over Objects in this case. You might well have an object from another context, for example.

But your starting index claim is not true either; if you don't set a property on an array it doesn't exist either, per keys() or in:

JavaScript code:
> "0" in [,1]
false
I spent months of my life implementing Array things and optimizing Array implementations, this conversation is sort of triggering.

qntm
Jun 17, 2009

Subjunctive posted:

JavaScript code:
> "0" in [,1]
false

You can just put a mess of unseparated commas in an array literal? That's new to me.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

qntm posted:

You can just put a mess of unseparated commas in an array literal? That's new to me.

Yeah, array literals can represent holes.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



I remember when some code of one of my coworkers was 'broken by' a new Chrome version when it stopped enumerating object properties in numeric (or maybe insertion?) order and he made a big deal about it because his objects-as-sparse-arrays wouldn't work right anymore. IIRC he 'solved' it by packing real arrays with false where there was no data and for some reason he was originally forming keys by multiplying the page number by 1000 and storing the items on the page in the first 1000 numbers which eventually collapsed when someone tried to display a 100-page document.

necrotic posted:

Right, but if you care about order why would you use an Object with numeric keys instead of an Array (which gives you forEach that is ordered, whereas Object.keys().forEach would not be)? The only case I could see using an Object over an Array is if your starting index isn't 0 or something (since every index before N would "exist" as undefined).

Because you're a PHP developer and that's how it works there so that's how every other language must work, right?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Munkeymon posted:

I remember when some code of one of my coworkers was 'broken by' a new Chrome version when it stopped enumerating object properties in numeric (or maybe insertion?) order and he made a big deal about it because his objects-as-sparse-arrays wouldn't work right anymore. IIRC he 'solved' it by packing real arrays with false where there was no data

This is especially ridiculous given that Arrays handle sparseness just fine.

no_funeral
Jul 4, 2004

why
CoffeeScript code:
//Some other stuff to get the item that I'm dragging
.sortable
    connectWith: '.patch'
    delay: 150
    revert: 0
    helper: (e, item) ->
      console.log item
      if !item.hasClass('selected')
        item.addClass('selected').siblings().removeClass 'selected'
      elements = item.parent().children('.selected').clone()
      item.data('multidrag', elements).siblings('.selected').remove()  <<<<<<<<----------- THIS LINE
      helper = $('<div></div>')
      helper.append elements
    stop: (e, ui) ->
      elements = ui.item.data('multidrag') <<<<<<<<----------- THIS LINE TOO
      ui.item.after(elements).remove()
This is a jqueryui question: in this sortable call, in the highlighted lines, am I crazy for thinking these are the same object? Using version jqueryui v1.10, trying to adapt an example from v1.9.

This is for being able to multiselect drag and drop items. The idea is you add the other selected elements to the initial objects data, then retrieve them once the drag release event (stop) fires. the ui.item.data('multidrag') call in the stop block is undefined, and appears to work just fine before the end of the custom helper function, i.e. if i check item.data('multidrag') while I'm still inside of the custom helper, I see it.

huhu
Feb 24, 2006
Thanks everyone for the help so far. Rewrote all the code, managed to actually get results. Haven't figured out what test situation will utterly destroy my code but we'll get there when we get there. However, this'll probably just end up being a way for me to track my progress in learning all the html elements and attributes and I won't purposefully try and break it, though I should.

https://htmlpreview.github.io/?https://github.com/TravisBumgarner/Codesaurus/blob/master/index.html

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Subjunctive posted:

Actually, engines switched to numeric order because it fit into optimized storage setups. Previously, arrays were objects in how they stored properties, and shared Objects' enumeration order (which was was property addition order by de facto standard, pending ES4 de jure standardization, and on that topic there is a lot of remaining compatibility baggage today). Moving to dense vectors for (most) Array storage meant either tracking property addition order separately, or enduring some compatibility breakage by enumerating numeric properties in numeric order regardless of the order in which they were added. I broke a few sites with Firefox 3 when I did that, but we decided to roll with it.

I definitely remember some V8 engineer complaining about how when they didn't enumerate arrays in numeric order, a lot of sites broke.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Suspicious Dish posted:

I definitely remember some V8 engineer complaining about how when they didn't enumerate arrays in numeric order, a lot of sites broke.

Possibly if they didn't enumerate numeric before non-numeric? Maybe sites have come to expect numeric order, but I didn't think IE always did that until 11.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Subjunctive posted:

But your starting index claim is not true either; if you don't set a property on an array it doesn't exist either, per keys() or in:

Ah, I could've sworn it was. Thanks for the correction.

Munkeymon posted:

Because you're a PHP developer and that's how it works there so that's how every other language must work, right?

I haven't worked with PHP in over 6 years, so no. rear end.


edit: My confusion on sparse arrays came from JSON-ifying them :downs:

necrotic fucked around with this message at 18:08 on May 21, 2015

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

necrotic posted:

edit: My confusion on sparse arrays came from JSON-ifying them :downs:

Yeah, JSON doesn't permit holes in arrays; I have no idea what the rationale was for that.

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
Doug Crockford is on record that he doesn't consider sparse arrays to be one of JavaScript's good parts, and besides that JSON targets languages other than JavaScript.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
is doug still on tc39 or have people realized that he's sort of a big hack by unironically inventing a new fixed number format in 2015?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

AFAIK he's still active.

Boz0r
Sep 7, 2006
The Rocketship in action.
If I have a type inheriting from another type, how do I find out which properties are defined on the parent or the child?

Kekekela
Oct 28, 2004

Boz0r posted:

If I have a type inheriting from another type, how do I find out which properties are defined on the parent or the child?

If I'm understanding you correctly you want hasOwnProperty.

...or If you don't care whether its inherited or not you can just do
if ('someprop' in myObj)

Boz0r
Sep 7, 2006
The Rocketship in action.
I'll check it out. I'm looking to get a list of the properties defined in the parent, and a list of the properties defined in the child.

EDIT: It doesn't work properly, I think I need some help.

code:
function Animal(name)
{
   this.eat = function() {alert("Eat");}
   this.name = name;
   
   return this;
}

function Dog(name) {
	Animal.call(this, name);
	this.bark = function() {alert("Bark");}
	this.wagTail = function() {alert("Wag Tail");}
	this.prototype = Object.create(Animal.prototype);
	return this;
}

Dog.prototype = Object.create(Animal.prototype);

var dog = new Dog("Bob");

var dogProperties = Object.getOwnPropertyNames(dog); 			// returns ["eat", "name", "bark", "wagTail"]
var animalProperties = Object.getOwnPropertyNames(dog.prototype); 	// returns []
dog.prototype; 								// returns Object {}
I'm just looking to make a function that, given a dog, gives me two lists of the properties defined in Dog and Animal. Any tips?

Boz0r fucked around with this message at 14:31 on May 24, 2015

Kekekela
Oct 28, 2004

Boz0r posted:

I'll check it out. I'm looking to get a list of the properties defined in the parent, and a list of the properties defined in the child.


Oh sorry, try Object.keys

Kekekela
Oct 28, 2004

Subjunctive posted:

AFAIK he's still active.

I'm actually just listening to an Eich presentation where he says that Crockford doesn't attend anymore.

https://www.youtube.com/watch?v=u56b_HUj47E

e: for some reason the time cue got messed up when SA converted the video link, it should be at 12m30s for the relevant bits

qntm
Jun 17, 2009
Is there any practical reason or circumstance where I should use

JavaScript code:
function a() {
    // do a thing
}
over

JavaScript code:
var a = function() {
    // do a thing
};
? I'm writing some introductory JavaScript materials and I trying to figure out if there's a single reason why I should even bother to teach the first kind of syntax, since it's self-explanatory when seen in the wild.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

qntm posted:

Is there any practical reason or circumstance where I should use

JavaScript code:
function a() {
    // do a thing
}
over

JavaScript code:
var a = function() {
    // do a thing
};
? I'm writing some introductory JavaScript materials and I trying to figure out if there's a single reason why I should even bother to teach the first kind of syntax, since it's self-explanatory when seen in the wild.

In ES6 you export functions from modules by naming them.

And you can use the name of the function to recurse.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Subjunctive posted:

This is especially ridiculous given that Arrays handle sparseness just fine.

Almost everyone at that job thought JavaScript worked just like C. It was an especially lovely environment to learn the language.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

qntm posted:

Is there any practical reason or circumstance where I should use

JavaScript code:
function a() {
    // do a thing
}
over

JavaScript code:
var a = function() {
    // do a thing
};
? I'm writing some introductory JavaScript materials and I trying to figure out if there's a single reason why I should even bother to teach the first kind of syntax, since it's self-explanatory when seen in the wild.

Function declarations are hoisted, while the assignment of a function expression's result to a variable only occurs when that line of code is reached. Assigning an anonymous expression result requires the developer to think about the ordering of their source in a way that simple declarations don't.

qntm
Jun 17, 2009

Wheany posted:

In ES6 you export functions from modules by naming them.

And you can use the name of the function to recurse.

Regarding your first point, does it count if I name functions like so?

JavaScript code:
var a = function func_name() {
    // do a thing
};
And for your second point, this works fine:

JavaScript code:
var fib = function(n) {
    return n === 0 ? 0 : n === 1 ? 1 : fib(n - 1) + fib(n - 2);
};
fib(13);

Subjunctive posted:

Function declarations are hoisted, while the assignment of a function expression's result to a variable only occurs when that line of code is reached. Assigning an anonymous expression result requires the developer to think about the ordering of their source in a way that simple declarations don't.

OK. Since in my second example the variable a would be hoisted (even if its value isn't), I think in practice that amounts to "don't call a function until it's been defined", which I can live with...?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

qntm posted:

OK. Since in my second example the variable a would be hoisted (even if its value isn't), I think in practice that amounts to "don't call a function until it's been defined", which I can live with...?

The variable being hoisted doesn't really help with anything, since there's no static checking of names. "Until it's been defined" can be hard to reason about, in the presence of things like onload scheduling. It doesn't always mean "later in source order", such as with mutually-recursive functions.

TBH I'm not sure why you'd want to introduce that complexity (and explain why you type the name twice, or what it means for variable and function names to be different) rather than start with the simpler and more common form.

Adbot
ADBOT LOVES YOU

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

qntm posted:

And for your second point, this works fine:

JavaScript code:
var fib = function(n) {
    return n === 0 ? 0 : n === 1 ? 1 : fib(n - 1) + fib(n - 2);
};
fib(13);

Yeah, that works, but if you name the function, recursion also works in situations like this:

JavaScript code:
var operations = [
	//...
	function blah(x) {
		//...
		blah(x - 1);
		//...
	}
];

operations[z](10);
JavaScript code:
someElement.on('someEvent', function blah(x) {
	//...
	blah(x - 1);
	//...
});
where the function isn't assigned to a variable you can access.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply