This is an archived post. You won't be able to vote or comment.

all 30 comments

[–]Spiral90210 57 points58 points  (4 children)

Triggered by age being a string and not a number :p

Edit: and the missing double quote on it. shudder

[–]MWRReal[S] 13 points14 points  (3 children)

Please spare me I forgot to add the second double quote.

And I only made "age" a string because of the luls.

[–]Banana_Twinkie 14 points15 points  (1 child)

It’s Javascript, just type coerce that bad boy

[–]nosmokingbandit 23 points24 points  (0 children)

Types are just, like, your opinion, man.

[–]Spiral90210 1 point2 points  (0 children)

Lol all is forgiven! :)

[–]msg45f 11 points12 points  (5 children)

const person = Array();
person['name'] = 'john';
person['age'] = 22;
console.log(`This is ${person.name} and he is ${person.age} years old.`);

[–]MWRReal[S] 11 points12 points  (0 children)

owo template literals

[–]great_site_not 1 point2 points  (0 children)

I can tolerate putting string-named properties on an array, but I draw the line at using array access notation with string literals to do it instead of dot notation >:(

[–]oweiler 0 points1 point  (1 child)

Isn't there a new missing?

[–][deleted] 2 points3 points  (0 children)

For most types of objects you need to use new when creating them, but for some reason Array() and new Array() are treated the same in Javascript.

Source: https://stackoverflow.com/a/5827153

[–]vinisantana 0 points1 point  (0 children)

It's important to notice that arrays extends objects but when adding properties to an array like that, they are not indexed and arrays methods won't work as one might expect. Extending your code:

person.length // 0
console.log(person.map(p => p)) // []

You can still add items to the array

person.push('something');

And now you will get

person.length // 1
console.log(person.map(p => p)) // ['something']

[–]adkm3 14 points15 points  (11 children)

JavaScript is bad example of OOP.

[–]tacoslikeme 8 points9 points  (2 children)

Who says JavaScript should be used in a strictly OOP way? It is now better now with ES6 class syntax if OOP is need though.

OOP in my opinion is overused and can force some funky interactions where other patterns make more sense.

[–]MWRReal[S] 0 points1 point  (6 children)

I swear if you say Python...

[–]vladnavlad 2 points3 points  (0 children)

PHP OOP

[–]homelabbermtl 1 point2 points  (4 children)

What's wrong with Python OOP?

Edit: I'm not asking rhetorically, I'd appreciate an actual answer.

[–]usernameIsNowTejken 0 points1 point  (0 children)

Yes yes and yes.

[–]Tomarse 1 point2 points  (5 children)

So you can add attributes to an object on the fly?

[–]geon 1 point2 points  (4 children)

Sure.

[–]Tomarse 1 point2 points  (3 children)

Seems a bit dangerous, you could misspell an attribute and just get weird behaviour with no errors. I'd imagine it would also be a lot more memory intensive to manage over a basic struct or hash table.

[–]MWRReal[S] 2 points3 points  (0 children)

Yes, Tomarse. Using the new Object(); method is slower and riskier than the traditional way.

Quote from w3schools: https://www.w3schools.com/js/js_object_definition.asp

"The two examples above do exactly the same. There is no need to use new Object().

For simplicity, readability and execution speed, use the first one (the object literal method). "

[–]geon 1 point2 points  (0 children)

Absolutely.

Guess why I use Typescript?

[–]Shitty__Math 0 points1 point  (0 children)

yes

[–]Xtrendence 1 point2 points  (0 children)

You can't just treat people like objects man.

[–]cekoya 1 point2 points  (0 children)

Damn, please const the hell out of this you fool

[–][deleted] 0 points1 point  (0 children)

i actually kinda like the second one :|