Javascript Is freaking CRAZY

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Some of the stuff that you can do with it are insane. For example, giving a class/element an attribute on the fly. Thats just insane. Or using a function pointer on a function with varying numbers of parameters. Its so loose in standards, no wonder people get driven nuts when they try to do something in C++ only to find "You can't do that."

Just saying. I got this revelation after hacking out this piece of code.

Code:
function ajaxUpdate(data)
{
	this.nextSel.empty();
	$(data).find('option').each(function ()
	{
		var ele = $(this);
		var name = ele.attr('name');
		var id = ele.attr('id');
		var Opt = $('<option></option>');
		Opt.text(id + ' - ' + name);
		Opt.attr('value', id);
		$('#exch').append(Opt);
	});
}

$(document).ready(function (){
			setupBrowser();
			setupLayout();
			setupAjax();
			$('#comp').each(function() {
				this.reqVal = 'ce';
				this.nextSel = $('#exch');
				});
			$('#comp').change(function ()
			{
				$.ajax({
					data: { 
						id: $(this).attr('value'),
						// To do: replace with real cookie
						sessionid: 'iifyqq1cg3kn', 
						request: this.reqVal
						},
					success: $.proxy(ajaxUpdate, this),
					error: function()
					{
					}
				});
			});
		});

Feel free to share your "This language is nuts." stories here as well.


(Also, Ignore the indenting... It is somewhat a "work in progress")
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Crazy flexible, yes, but that doesn't exactly make for easy maintainability. And closures are real easy to screw yourself over in scope. For example:

Code:
var X=2;
someelement.addHandler('click', function(){alert X;}, false);
X = 3;
someotherelement.addHandler('click', function(){alert X;}, false);

will have both elements popping up 3. but...

Code:
var X=2;
ElementHandler(someelement, X);
X = 3;
ElementHandler(someotherelement, X);

function ElementHandler(element, index)
{
element.addHandler('click', function(){alert index;}, false);
}

will have them pop up 2 and 3 respectively. Dangerous when setting closures from a loop.


That's not just javascript you've got there though. It's absolutely hooked up to a framework, probably jQuery.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
PHP, Perl and Flash ActionScript can be "fun" like that too.

I prefer weird C/C++ pointer madness over loosely typed dynamic interpreted languages any day. Not always having access to single-step debugging to see why it's going horribly wrong is just the icing on the cake.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
PHP, Perl and Flash ActionScript can be "fun" like that too.

I prefer weird C/C++ pointer madness over loosely typed dynamic interpreted languages any day. Not always having access to single-step debugging to see why it's going horribly wrong is just the icing on the cake.

Oh no, the icing on the cake is "This works on IE 7, but not IE 6"

Dealing with old IE and all its quirks is terrible. I wish IE6 and lower would just die. Unfortunately, the project I'm working on will be for people that are using Win2000 and IE6...
 

iCyborg

Golden Member
Aug 8, 2008
1,327
52
91
I worked with JS for a while, and there were moments I hated its guts. I'm sure it's fun if you're playing with it on your own, but I was working on a large piece of code that I didn't write and had deadlines, not to mention that this was the first time I worked with JS, and FFS it took me ages to understand what's going on sometimes and why is some code needed at all. C/C++/C# seem so much more organized and understandable.
Also, keyword 'this' seems to be very important in JS, it's fu**ing everywhere...
 

Ka0t1x

Golden Member
Jan 23, 2004
1,724
0
71
That looks like standard jQuery to me. And yes I do love how flexible it is.
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
PhatoseAlpha it's not too mysterious if you think about what's going on. In the first example you closed over a binding and then modified it. In the second example, you forced evaluation (call-by-value) by invoking that function, so you got whatever the value of the variable X was at the point of the call.

And shit, I'm really drunk and I can pick that out.
 

Martin

Lifer
Jan 15, 2000
29,178
1
81
Javascript's functional side is pretty awesome, hard to go back after using it for a while.
 

Sgraffite

Member
Jul 4, 2001
88
0
66
I thought it was interesting when I found out modifying the prototype of an object affects all instances of that object.

Also creating objects and having them extend other objects can be fun in javascript

It can be frustrating, but once you understand the quirks and what certain error messages mean in different browsers it can be a quite enjoyable language. Also jQuery has made my life a lot easier, prototype I'm not a fan of however.

It is pretty amazing how powerful it is considering it was created by Netscape, however it has undergone a lot of changes since then.

There are also libraries that allow you to write java and it gets converted into javascript. This lets you share objects between the languages from what I hear.
 
Last edited:

bobsmith1492

Diamond Member
Feb 21, 2004
3,875
3
81
Matlab is basically Javascript, isn't it? It's fun to build variable names and create them on the fly, like:

for i=1:10
runString = sprintf('varName&#37;d = %d*42;',i,i);
eval(runString);
end

Totally crazy coming from a very static assembly and C world...

Also, Sgraffite - there are contests here for people who have been around the longest with the fewest posts. I'm pretty sure you'd win.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,282
3,904
75
Adding variables on the fly is a common scripting language attribute. Adding class members, maybe not so much.

No, Matlab is different crazy. It's a language that's only fast when you use SIMD. Instead of looping to multiply two arrays together, you can just say A.*B.

Of course normal loops work too. But since it's so slow the other way, you really have to think SIMD when solving problems.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Adding variables on the fly is a common scripting language attribute. Adding class members, maybe not so much.

No, Matlab is different crazy. It's a language that's only fast when you use SIMD. Instead of looping to multiply two arrays together, you can just say A.*B.

Of course normal loops work too. But since it's so slow the other way, you really have to think SIMD when solving problems.

Adding datamembers on the fly is what gets me the most. Its like "Humm, I need an attribute on this object to tell if it has been used or not.. How about I just use it!"

Well, that and being able to call functions with a different call signatures then said function.
 
Oct 27, 2007
17,010
1
0
Adding members at run time is a common feature across most dynamic languages. I don't see the appeal personally, it seems likely to cause more debugging nightmares than anything. Not that I'm a dynamic expert, but give me nice statically type, late-bound languages like C# and Java or powerful and expressive functional languages like F# (still strongly typed) any day. Dynamic typing makes me want to harm myself.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |