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
Vanadium
Jan 8, 2005

But the interpreter is going to add more ; anyway, it is not like you are going to convince it that you know your poo poo and it does not need to put a ; after return

Adbot
ADBOT LOVES YOU

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Vanadium posted:

But the interpreter is going to add more ; anyway, it is not like you are going to convince it that you know your poo poo and it does not need to put a ; after return

OK Mr. Real Programing Tough Guy, I see the error of my ways! You should email everyone to make sure they know.

NotShadowStar
Sep 20, 2000
Lua and Ruby the semi-colon is defined as optional. ECMAScript the semicolon is not-optional, but the interpreter tries to fix this for you, and is usually wrong.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

NotShadowStar posted:

Lua and Ruby the semi-colon is defined as optional. ECMAScript the semicolon is not-optional, but the interpreter tries to fix this for you, and is usually wrong.

Now way, you are just trying to impress people with those semi-colons dude. If you used real language, you'd know what a n00b you are. I bet you even indent your code... the interpreter just ignores that whitespace, so why bother putting it in!!!

There Will Be Penalty
May 18, 2002

Makes a great pet!

NotShadowStar posted:

Lua and Ruby the semi-colon is defined as optional. ECMAScript the semicolon is not-optional, but the interpreter tries to fix this for you, and is usually wrong.

ECMA-262 posted:

7.9 Automatic Semicolon Insertion

OddObserver
Apr 3, 2009
7.9 should get some sort of special award for most-unlike-RFC2119 use of 'must' and 'may' in a computer-related spec:

quote:

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations.

More substantially, however, semicolon insertion is really worst of both worlds: some of the time it happens as parse error recovery, and some of the time the interpreter is required to infer a semi-colon in a way that just emulates a combination of enforcing a bizarre restriction on whitespace with parse error recovery.

dark_panda
Oct 25, 2004
Try using a JavaScript minifier and concatenator with missing semi-colons. Some, like YUI Compressor, fare better than others because they run the code through an actual JavaScript interpreter so they can insert semi-colons where necessary when compressing the code, but others like jsmin just try and remove whitespace which can really blow things up when line returns are removed.

The optional use of semi-colons in ECMAScript was kind of a bad decision because of network concerns I guess. Ruby and Lua aren't really meant to be served out across networks where minification can help reduce file size and get things to the user quicker, but in JavaScript this is a concern, so treating semi-colons as optional is not a good idea. Just pretend that that part of the spec doesn't exist and use semi-colons religiously, and dammit use jslint and other JavaScript linters. They will make you produce better code, period.

NotShadowStar
Sep 20, 2000

There Will Be Penalty posted:



code:
return
{
  fart: 'butts'
};
Oh hay what is go--

Vanadium
Jan 8, 2005

Where in that example would putting an explicit semicolon fix anything

NotShadowStar
Sep 20, 2000
It doesn't fix anything, automatic insertion of semicolons break the statement completely and forces you into a coding style.

Here's how it looks after semicolon insertion

code:
return;
{
  fart: 'butt'
};
You fix it by doing a coding style you might not be comfortable with if you like your braces lined up, just to get around semicolon insertion, something you should never have to do
code:
return {
  fart: 'butt'
};
If you're not even aware of semicolon insertion this would drive you absolutely mad, because in something that isn't batshit crazy it's a valid statement.

Vanadium
Jan 8, 2005

Ruby forces you to do ary.each do { derp } and does not allow ary.each do
{
  derp
}
either :shobon:

dark_panda
Oct 25, 2004

Vanadium posted:

Ruby forces you to do ary.each do { derp } and does not allow ary.each do
{
  derp
}
either :shobon:

You can't do both the "do" statement and curly braces together for blocks in Ruby, it has to be one or the other. What you have there is a syntax error.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

dark_panda posted:

You can't do both the "do" statement and curly braces together for blocks in Ruby, it has to be one or the other. What you have there is a syntax error.

Give him a break, he's trying to pick a e-fight with us "lesser" programmers; facts are irrelevant. (Hmm, I wonder if that semicolon in my sentence was superfluous.)

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker
If you have to ask, it already is.

Vanadium
Jan 8, 2005

dark_panda posted:

You can't do both the "do" statement and curly braces together for blocks in Ruby, it has to be one or the other. What you have there is a syntax error.

Fine, remove the do

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Vanadium posted:

Fine, remove the do

So what exactly was your javascript question?

NotShadowStar
Sep 20, 2000

Vanadium posted:

Ruby forces you to do ary.each do { derp } and does not allow ary.each do
{
  derp
}
either :shobon:

Because that's ambiguous. The Ruby VM does not and will not ever try and figure out what you intended. It's the same as optional parentheses in Ruby until it becomes ambiguous. For example
code:
puts "Puts is a method on Kernel, but there is no ambiguity so this is okay"
puts "This statement is totally ambiguous and the interpreter will error " + ["e", "r", "r", "o", "r"].join ''
So what does this have to do with do and {? Because of the same thing here
code:
Hash.new {:a => b}
Why can't you have a method with a has parameter without parentheses? Because there is no lexical distinction between the hash literal and the block literal, and the Ruby VM will never ever guess.

So what about 'do' then? The problem comes from newlines are taken as statement terminators in Ruby unless explicitly escaped with \. When you terminate a statement like [1, 2, 3].each(), each is actually returning an Iterator that is normally handled by the block.
code:
z = [1, 2, 3].each
puts z.class #=> Enumerable:::Enumerator
So when you do
code:
[1, 2, 3].each
do |n|
end
The compiler is really confused because YOU TERMINATED THE STATEMENT. It's DONE, so there should be no 'do' keyword, because there is nothing to accept a block, it is a bare block. If you really want, you can do
code:
[1, 2, 3].each \
 do |n|
 puts n
end
And that works fine.

tl;dr Ruby is a much smarter design because it never attempts to guess what you do, the automatic semicolon insertion is bad because it attempts to be smart at what you're doing and will fail.

Vanadium
Jan 8, 2005

Both ruby and js put ; at the end of every line unless there is like a , there or an open ( somewhere or whatever!

Why is it a big deal for one of them and not the other?

There Will Be Penalty
May 18, 2002

Makes a great pet!
I believe it's safe to say that automatic semicolon insertion is invoked when it *can* be, i.e., when it won't cause a syntax error to do so at EOL.

It will not be invoked after an infix operator at EOL. It will also not be invoked if your innermost matching brackets are parenthesis, square brackets, or curly brackets used for object literal notation. I use these as rules of thumb about where it's safe to insert a newline (because gently caress a bunch of lines that are more than 80 columns long). So there's no reason to worry about any of the following statements:
code:
foo.buttz(
        balls +
        dooky
);
var buttz = {
        farts: "brrrt",
        balls: "cocks"
};
buttz.balls.cocks[
        i * i + j * j
] = 5;
It *will* be invoked if your innermost matching brackets are the curly brackets surrounding the body of a function, of course:
code:
foo.buttz(function () {
        x = 5           // it's invoked here
        + 6;
});
EDIT: it appears I was factually wrong as well. :bang:

There Will Be Penalty fucked around with this message at 06:43 on Jan 16, 2011

NotShadowStar
Sep 20, 2000

Vanadium posted:

Both ruby and js put ; at the end of every line unless there is like a , there or an open ( somewhere or whatever!

Why is it a big deal for one of them and not the other?

You're just posting poo poo and not even reading.

Ruby does not put a semicolon at the end of a line. Ruby has two ways of terminating a statement, semicolon character and end of line character (technically two of them for UNIX and DOS EOL characters).

puts "something"
and
puts "something";

are NOT exactly the same. In the first one the parser tree it looks something like (drastically simplified)

statement_begin method_call(arguments) end_statement
The second one reads
statement_begin method_call(arguments) end_statement statement_begin end_statement

If you actually read instead of vomiting bullshit, you'd be astute enough to note that you can break lines with Ruby using \
If you were even more astute, you'd realize that \ is special... hm...
Oh! Knowing that the end of line characters (\n, \r\n) are statement terminators, and the \ character is an escape character... you're escaping the statement terminator!

tl;dr again everything you have posted has been quite factually wrong.
tl;dr v2 Shut up kid, men are talking.

NotShadowStar fucked around with this message at 06:22 on Jan 16, 2011

OddObserver
Apr 3, 2009

There Will Be Penalty posted:

I believe it's safe to say that automatic semicolon insertion is invoked when it *can* be, i.e., when it won't cause a syntax error to do so at EOL.

Why speculate? See ECMA-262-5, 7.9, as was cited before for exact spec. And you're pretty much backwards --- it's only invoked when the program would not parse otherwise (with the bizarro provision that some statements and expressions don't permit a line break before/after their arguments)
<snip>


There Will Be Penalty posted:

] = 5;[/code]It *will* be invoked if your innermost matching brackets are the curly brackets surrounding the body of a function, of course:
code:
foo.buttz(function () {
        x = 5           // it's invoked here
        + 6;
});

Nope, it's not. x is 11.

Vanadium
Jan 8, 2005

well gently caress if the parser tree says so

(USER WAS PUT ON PROBATION FOR THIS POST)

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Vanadium posted:

well gently caress if the parser tree says so

Could you just stop making GBS threads up the thread please? I'm sorry you don't like yourself and feel the need to put down us poor saps who use javascript and add semicolons where we technically don't need them so you can look in the mirror and see a big man, but since you've done nothing but show how ignorant you are, you might want to find somewhere else to troll.

Evil Robot
May 20, 2001
Universally hated.
Grimey Drawer
So here's a dumb question:

I am trying to use Google's Blobstore (http://code.google.com/appengine/docs/python/blobstore/overview.html). In order to upload data, they require you post to a serving URL that you embed in the client's JavaScript (yay, got that) and that the data come from a form with an input type="file" element. Now, what I really want to send is some data I have in a JavaScript string variable. Is it possible for me to POST (with enctype="multipart/form-data") that data somehow?

Basic Googling seems to indicate this is not really possible with any of JQuery's AJAXy functions, but that Dojo might have some promise (http://dojotoolkit.org/reference-guide/dojo/io/iframe.html#dojo-io-iframe). However, I can't really see how to put the string data I have into a generated input="file" form. Is this just not possible? If so, why is this stuff not possible?

NotShadowStar
Sep 20, 2000

Vanadium posted:

well gently caress if the parser tree says so

I'm starting to see why the Minecraft threads always have deadly titles if these are the people that come from them.

Haystack
Jan 23, 2005





Evil Robot posted:

So here's a dumb question:

I am trying to use Google's Blobstore (http://code.google.com/appengine/docs/python/blobstore/overview.html). In order to upload data, they require you post to a serving URL that you embed in the client's JavaScript (yay, got that) and that the data come from a form with an input type="file" element. Now, what I really want to send is some data I have in a JavaScript string variable. Is it possible for me to POST (with enctype="multipart/form-data") that data somehow?

Basic Googling seems to indicate this is not really possible with any of JQuery's AJAXy functions, but that Dojo might have some promise (http://dojotoolkit.org/reference-guide/dojo/io/iframe.html#dojo-io-iframe). However, I can't really see how to put the string data I have into a generated input="file" form. Is this just not possible? If so, why is this stuff not possible?

With jQuery, the following should allow you to set the right header:

code:
jQuery.ajax({
    beforeSendL: function(XHR, settings){
        //This is the part that sets the header
        //Note that XHR is an actual factual XMLHttpRequest object
        XHR.setRequestHeader("enctype","multipart/form-data")
    },
    data: {
        blob: "your_string_goes_here"
    },
    type: "post",
    url: "http://code.google.com/appengine/docs/python/blobstore/whatever"
}
Note that I haven't actually checked that this works.

Parantumaton
Jan 29, 2009


The OnLy ThInG
i LoVe MoRe
ThAn ChUgGiNg SeMeN
iS gEtTiNg PaId To Be A
sOcIaL MeDiA sHiLl
FoR mIcRoSoFt
AnD nOkIa
File upload can't be done through Ajax alone, you need to use an iframe or Flash to make it look like it's something Ajaxy.

Evil Robot
May 20, 2001
Universally hated.
Grimey Drawer

Parantumaton posted:

File upload can't be done through Ajax alone, you need to use an iframe or Flash to make it look like it's something Ajaxy.

Yeah this is the problem.

Munkeymon
Aug 14, 2003

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



Lumpy posted:

code:
// I assume 'snapshotLength' is a built in property or something as it's never defined....
                if (myCount < evil.snapshotLength) {  
                ...
	event.preventDefault();
	evil = document.evaluate("TBODY/TR/TD/DIV/A[contains(@class,'count')]",
	            document.getElementById("forum"), null, 7, null);

snapshotLength is the number of items in the snapshot returned by the document.evaluate call that's doing an XPath query on the current DOM. I don't know for sure why they don't stuff them into a regular array, but I'd guess they wanted to make it extra clear that you can't just add more nodes into the result of evaluate and expect them to show up in the DOM.

https://developer.mozilla.org/en/XPathResult

It's pretty neat because you can use it like jQuery's selectors* when you write a user script for browsers that aren't total garbage.

*Actually, I read that jQuery's selector engine just uses XPath queries on browsers that support them, but have been to lazy to verify

Nigglypuff
Nov 9, 2006


BUY ME BONESTORM
OR
GO TO HELL
Pretty sure the XPath selector backend was removed from jQuery, since the browsers who supported it all support document.querySelectorAll now anyway.

And since we're on the subject of jQuery:
code:
// ==UserScript==
// @name           SA: Open New Posts in New Tabs
// @include        htt*://forums.somethingawful.com/*
// ==/UserScript==

var $ = unsafeWindow.jQuery;

var button = $("<a href=# style='float:right;margin-right:8px'>Open New Posts in New Tabs</a>");

button.prependTo("th.title").click(function (event) {
	event.preventDefault();
	var nodes = $(".count").toArray();
	new function() {
		if (! nodes.length) return;
		GM_openInTab(nodes.shift().href);
		setTimeout(arguments.callee, 110);
	};
});

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Nigglypuff posted:

Pretty sure the XPath selector backend was removed from jQuery, since the browsers who supported it all support document.querySelectorAll now anyway.

And since we're on the subject of jQuery:
code:
// ==UserScript==
// @name           SA: Open New Posts in New Tabs
// @include        htt*://forums.somethingawful.com/*
// ==/UserScript==

var $ = unsafeWindow.jQuery;

var button = $("<a href=# style='float:right;margin-right:8px'>Open New Posts in New Tabs</a>");

button.prependTo("th.title").click(function (event) {
	event.preventDefault();
	var nodes = $(".count").toArray();
	new function() {
		if (! nodes.length) return;
		GM_openInTab(nodes.shift().href);
		setTimeout(arguments.callee, 110);
	};
});

Look at all those useless semicolons. :colbert:

Serious question: why the use of new function() and arguments.callee instead of a local var that's assigned the function and then a reference to that in the timeout? I know what you are doing is effectively equivalent, but interested if there is an advantage to doing it your way.

fishmech
Jul 16, 2006

by VideoGames
Salad Prong

Nigglypuff posted:

Pretty sure the XPath selector backend was removed from jQuery, since the browsers who supported it all support document.querySelectorAll now anyway.

And since we're on the subject of jQuery:
code:
// ==UserScript==
// @name           SA: Open New Posts in New Tabs
// @include        htt*://forums.somethingawful.com/*
// ==/UserScript==

var $ = unsafeWindow.jQuery;

var button = $("<a href=# style='float:right;margin-right:8px'>Open New Posts in New Tabs</a>");

button.prependTo("th.title").click(function (event) {
	event.preventDefault();
	var nodes = $(".count").toArray();
	new function() {
		if (! nodes.length) return;
		GM_openInTab(nodes.shift().href);
		setTimeout(arguments.callee, 110);
	};
});

That doesn't work in the latest Firefox 4, for what it's worth. Same problem with only opening the first thread as the original code had.

epswing
Nov 4, 2003

Soiled Meat

Lumpy posted:

Look at all those useless semicolons. :colbert:

And all that whitespace :rolleyes:

Lumpy posted:

Serious question: why the use of new function() and arguments.callee instead of a local var that's assigned the function and then a reference to that in the timeout? I know what you are doing is effectively equivalent, but interested if there is an advantage to doing it your way.

You're just pretending not to know something in an attempt to fit in with the rest of us. :colbert:

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

What the hell does "new function (){...};" even do? Don't you have to actually call the function like so: "new (function() {...})();" ?

OddObserver
Apr 3, 2009

peepsalot posted:

What the hell does "new function (){...};" even do? Don't you have to actually call the function like so: "new (function() {...})();" ?

It calls the anonymous function/function expression as a constructor --- so 'this' is set to a fresh object inside the function (since the function object doesn't have .prototype), and the entire expression evaluates to that object.

So basically the same as:
var tmp = function () {
....
}

new tmp;

... just without storing it.

Edit: change example to be closer.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

epswing posted:

You're just pretending not to know something in an attempt to fit in with the rest of us. :colbert:

:ssh:

Most folks don't know to do crazy stuff like that, so explaining it would probably be helpful to most.

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

OddObserver posted:

It calls the anonymous function/function expression as a constructor --- so 'this' is set to a fresh object inside the function (since the function object doesn't have .prototype), and the entire expression evaluates to that object.

So basically the same as:
var tmp = function () {
....
}

new tmp;

... just without storing it.

Edit: change example to be closer.
I understand the concept of an anonymous function expression. I just didn't think you could call new like that: "new tmp;" I've always seen it as "new tmp();". Are thoe two statements equivalent?

Insurrectum
Nov 1, 2005

I'm writing a script that finds a phrase in a page and then determines if the previous link matches one in an array of objects, and if it doesn't adds that link to the end of that array. For some reason it isn't working, however. It finds the phrase but doesn't collect the link. Can anyone see what's going wrong?

code:
function searchpage() {
	var grabbedElems = new Array();
	var grabbedLinks = new Array();
	var bElems = document.getElementsByTagName('a');
	for (var i = 0; i < bElems.length; i++) {
		if (bElems[i].innerHTML == “phrase”) {
			if (grabbedElems.indexOf(bElems[i-1].innerHTML) == -1) {
				grabbedElems.push(bElems[i-1].innerHTML);
				grabbedLinks.push(bElems[i-1]);
			}
		}
	}
}

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Insurrectum posted:

I'm writing a script that finds a phrase in a page and then determines if the previous link matches one in an array of objects, and if it doesn't adds that link to the end of that array. For some reason it isn't working, however. It finds the phrase but doesn't collect the link. Can anyone see what's going wrong?

code:
function searchpage() {
	var grabbedElems = new Array();
	var grabbedLinks = new Array();
	var bElems = document.getElementsByTagName('a');
	for (var i = 0; i < bElems.length; i++) {
		if (bElems[i].innerHTML == “phrase”) {
			if (grabbedElems.indexOf(bElems[i-1].innerHTML) == -1) {
				grabbedElems.push(bElems[i-1].innerHTML);
				grabbedLinks.push(bElems[i-1]);
			}
		}
	}
}

Don't use new Array() It makes baby jeebus cry. You will also trying to access the -1 element of bElms the first time through your loop, so that might be the cause of some woes.

EDIT: use varName = [] instead.

Lumpy fucked around with this message at 22:15 on Jan 17, 2011

Adbot
ADBOT LOVES YOU

Insurrectum
Nov 1, 2005

Lumpy posted:

Don't use new Array() It makes baby jeebus cry. You will also trying to access the -1 element of bElms the first time through your loop, so that might be the cause of some woes.

EDIT: use varName = [] instead.

I changed the array, but there's no way I'm going to be accessing bElems[-1], since I know the first instance of a link I'm looking for always occurs well into the document. (the first if condition makes sure of that)

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