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
Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

Condiv posted:

What? When I define a function, I want it to react pretty predictably to inputs given. I'd rather not allow for a whole new set of errors to be allowed by having "this" resolve to whatever the function happens to have been assigned to at the moment.

so don't use "this" or use _.bind on your function.

Adbot
ADBOT LOVES YOU

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror
you would have already known to do that if you knew anything at all about javascript

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Condiv posted:

What? When I define a function, I want it to react pretty predictably to inputs given. I'd rather not allow for a whole new set of errors to be allowed by having "this" resolve to whatever the function happens to have been assigned to at the moment.

a function in javascript is an object, just like "a string", the number 5, or a hash table

you can store functions in anything that accepts an object, such as arrays or hash tables

h.f(arg) is simply syntactic sugar for h['f'].call(h, arg)

code:
var f = function(){console.log(this);};
var g = {};
g['x'] = f;
f(); // => {}
g.x(); // => { x: [Function] }
g['x'].call(g); // => { x: [Function] }
this is actually valuable though, since you can build up a collection of functions in one hash, and then copy them one-by-one into some other object to implement mixins: https://github.com/sstephenson/prototype/blob/master/src/prototype/lang/class.js#L83


if you don't want that, exploit that js functions are closures and bind "this" in:
code:
var f = function(){console.log(this);};
var g = {};
g['x'] = function(){f.call({other: 'this'});};
g.x(); // => { other: 'this' }
in coffeescript, this is done using a fat arrow:
code:
Account = (customer, cart) ->
  @customer = customer
  @cart = cart

  $('.shopping_cart').bind 'click', (event) =>
    @customer.purchase @cart

// compiles to

var Account;

Account = function(customer, cart) {
  var _this = this;
  this.customer = customer;
  this.cart = cart;
  return $('.shopping_cart').bind('click', function(event) {
    return _this.customer.purchase(_this.cart);
  });
};
load
or add a bind method to Function: https://github.com/sstephenson/prototype/blob/master/src/prototype/lang/function.js#L45

in conclusion, quit pretending js isn't a dynamic language

Condiv
May 7, 2008

Sorry to undo the effort of paying a domestic abuser $10 to own this poster, but I am going to lose my dang mind if I keep seeing multiple posters who appear to be Baloogan.

With love,
a mod


Tiny Bug Child posted:

so don't use "this" or use _.bind on your function.

Yep, use more libraries to paper over the flaws in the language. That or ignore it.


Tiny Bug Child posted:

you would have already known to do that if you knew anything at all about javascript

Actually, nowadays I just use private members in classes with getter and setter functions when I need them.

quote:

if you don't want that, exploit that js functions are closures and bind "this" in

"this" should be bound by default considering js functions are closures.

Condiv fucked around with this message at 01:43 on Jul 12, 2012

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror
if someone doesn't see why that's a thing about javascript that rules, dropping a whole bunch of words on they dumb rear end won't help. bet you he wishes js had classical inheritance too

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Tiny Bug Child posted:

if someone doesn't see why that's a thing about javascript that rules, dropping a whole bunch of words on they dumb rear end won't help. bet you he wishes js had classical inheritance too

you can add it

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

Condiv posted:

Yep, use more libraries to paper over the flaws in the language. That or ignore it.


Actually, nowadays I just use private members in classes with getter and setter functions when I need them.

lolll

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror
this gosh darn language is just too flexible and expressive. let me mangle it until it looks like java

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug
i'll bet he's hand-writing accessors instead of having written a function to write them

code:
var attrAccessor = function(n){
  var _this = this;
  this['get'+n] = function(){return _this[n]};
  this['set'+n] = function(val){return _this[n] = val};
}
var g = { attrAccessor: attrAccessor };
g.attrAccessor('x');
g.setx(5); // => 5
g.getx(); // => 5

Zizzyx
Sep 18, 2007

INTERGALACTIC CAN CHAMPION

yaoi prophet posted:

JavaScript code:
Yospos.prototype.smoke = function(weed) {
    weed.smoke();
    var self = this;
    setTimeout(86400, function() { self.smoke(weed) });
}

is there anything special about the value 86400, other than it being sqrt(2) minutes? i feel like i've seen it elsewhere

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Zizzyx posted:

is there anything special about the value 86400, other than it being sqrt(2) minutes? i feel like i've seen it elsewhere

60 * 60 * 24 // => 86400

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug
never mind that setTimeout takes ms and not s https://developer.mozilla.org/en/window.setTimeout

Opinion Haver
Apr 9, 2007

smoke weed 1000 times a day

also smh if you don't know the number of seconds and minutes in a day by heart

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

yaoi prophet posted:

smoke weed 1000 times a day

also smh if you don't know the number of seconds and minutes in a day by heart

i don't, why should i when multiplication is cheap and "1000 * 60 * 60 * 24" makes it obvious what i mean

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

yaoi prophet posted:

smoke weed 1000 times a day

also smh if you don't know the number of seconds and minutes in a day by heart

I bet you're memory would have failed you on June 30 and Dec 31

Opinion Haver
Apr 9, 2007

because you might see it in other places that aren't source code

Condiv
May 7, 2008

Sorry to undo the effort of paying a domestic abuser $10 to own this poster, but I am going to lose my dang mind if I keep seeing multiple posters who appear to be Baloogan.

With love,
a mod


BonzoESC posted:

i'll bet he's hand-writing accessors instead of having written a function to write them

code:
var attrAccessor = function(n){
  var _this = this;
  this['get'+n] = function(){return _this[n]};
  this['set'+n] = function(val){return _this[n] = val};
}
var g = { attrAccessor: attrAccessor };
g.attrAccessor('x');
g.setx(5); // => 5
g.getx(); // => 5

That code creates or modifies public members, which kinda defeats the purpose of a getter/setter method doesn't it?

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Condiv posted:

That code creates or modifies public members, which kinda defeats the purpose of a getter/setter method doesn't it?

does javascript have "private members?"

what's the point of private members in a language that lets you add methods to an object?

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

BonzoESC posted:

i don't, why should i when multiplication is cheap and "1000 * 60 * 60 * 24" makes it obvious what i mean

cause

php:
<?
class butt { 
  const SECONDS_PER_DAY = 1000 * 60 * 60 * 24;
}
?>
is a parse error

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Condiv posted:

Yep, use more libraries to paper over the flaws in the language. That or ignore it.


Actually, nowadays I just use private members in classes with getter and setter functions when I need them.


"this" should be bound by default considering js functions are closures.

LOFL this whole post

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Tiny Bug Child posted:

cause

php:
<?
class butt { 
  const SECONDS_PER_DAY = 1000 * 60 * 60 * 24;
}
?>
is a parse error

LOFL this whole post

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>
wow looks like it's javascript ftl after all

Condiv
May 7, 2008

Sorry to undo the effort of paying a domestic abuser $10 to own this poster, but I am going to lose my dang mind if I keep seeing multiple posters who appear to be Baloogan.

With love,
a mod


BonzoESC posted:

does javascript have "private members?"

what's the point of private members in a language that lets you add methods to an object?

code:

function AnObject() {
  var x = 0; //private member
  this.y = 1; //public member
}
The point is trying to make sure things remain stable and functional (as futile as JS may make that).

Condiv fucked around with this message at 02:19 on Jul 12, 2012

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>
weird how lua doesn't have that strange problem with "this"

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Condiv posted:

[code]

function AnObject() {
var x = 0; //private member
this.y = 1; //public member
}

The point is trying to make sure things remain stable and functional.

so write tests

compiler's not gonna save you here

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

ahhh spiders posted:

weird how lua doesn't have that strange problem with "this"

of the two languages that live in web browsers, js is so much better than vbscript it's not even funny

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

BonzoESC posted:

of the two languages that live in web browsers, js is so much better than vbscript it's not even funny
oh it's funny

vbscript is the best thing because you know anyone who ever lists it on their resume is terrible

vapid cutlery
Apr 17, 2007

php:
<?
"it's george costanza" ?>

BonzoESC posted:

of the two languages that live in web browsers, js is so much better than vbscript it's not even funny

would b cool if browsers ran lua

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
browsers should only accept C

if you can't handle it get off the web

Condiv
May 7, 2008

Sorry to undo the effort of paying a domestic abuser $10 to own this poster, but I am going to lose my dang mind if I keep seeing multiple posters who appear to be Baloogan.

With love,
a mod


BonzoESC posted:

so write tests

compiler's not gonna save you here

I meant by using encapsulation.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

ahhh spiders posted:

would b cool if browsers ran lua

totes

coaxmetal
Oct 21, 2010

I flamed me own dad

Tiny Bug Child posted:

cause

php:
<?
class butt { 
  const SECONDS_PER_DAY = 1000 * 60 * 60 * 24;
}
?>
is a parse error

well that's just because php sucks, in any language worth poo poo that has a const k/w it can still evaluate an expression on first assignment.


If thats a parse error that gives some insight into how poorly the php grammar is constructed.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Condiv posted:

I meant by using encapsulation.

it's not a magick wand that makes your code "stable and functional", it's just a way to make it more inconvenient to write

if you want to know that your programs work, define what "work" means by writing tests, and have the tests run on all your code

Condiv
May 7, 2008

Sorry to undo the effort of paying a domestic abuser $10 to own this poster, but I am going to lose my dang mind if I keep seeing multiple posters who appear to be Baloogan.

With love,
a mod


BonzoESC posted:

it's not a magick wand that makes your code "stable and functional", it's just a way to make it more inconvenient to write

if you want to know that your programs work, define what "work" means by writing tests, and have the tests run on all your code

Of course encapsulation doesn't solve stability problems, it just makes them easier to manage. I don't agree that it makes code more inconvenient though.

Oh and I know about unit testing.

quote:

would b cool if browsers ran lua

Condiv fucked around with this message at 02:46 on Jul 12, 2012

CaptainMeatpants
Jun 1, 2010

javascript is pretty cool, i enjoy it for the most part

thank you

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
javascript is fine if youre not a keyboard cowboy

Sweeper
Nov 29, 2007
The Joe Buck of Posting
Dinosaur Gum
css is the worst

CaptainMeatpants
Jun 1, 2010

yes it rly is

Condiv
May 7, 2008

Sorry to undo the effort of paying a domestic abuser $10 to own this poster, but I am going to lose my dang mind if I keep seeing multiple posters who appear to be Baloogan.

With love,
a mod


Sweeper posted:

css is the worst


trex eaterofcadrs posted:

javascript is fine if youre not a keyboard cowboy

Yeah, it's ok. Just some things about it bug me ("this", silent failures).

Adbot
ADBOT LOVES YOU

coaxmetal
Oct 21, 2010

I flamed me own dad

Sweeper posted:

css is the worst

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

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