Author Topic: Javascript Array Remove Object  (Read 1505 times)

0 Members and 1 Guest are viewing this topic.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Javascript Array Remove Object
« on: February 17, 2014, 06:58:25 pm »
I know Python and am delving into other languages for purposes you know. Javascript to let you know if the title didn't already.

in python we can do:
Code: [Select]
x = ["a", "b", "c", "d"]
x.remove("c")

And it removes that object. Yes it is a string, but with scripting it is also an object.

And it works, we can pop it and other such things that "can and are" simpler.

Why do I need an to iterate the Array and slice in JS? Why is there no .remove() or other such utilities outside of my doing it myself, no libs involved? This is a scripting language right? This is even dynamically typed, so why do I need to treat this like C or Java? Everything is an object, I get that, but don't objects have methods? This object is just a datatype though. There are no built-ins for it? Where are the mutable data-types, such as something that needs it like an "List"? Hell, make a mutable Array. This is scripting, not bare metal....

Or maybe stackoverflow isn't telling me something, I;ve searched maybe ntot hard enough but still. Is there a mutable datatype that can hold "objects" or a simple listt [in python notation] that can hold objects I can index by 'name' to operate on, whether it can be removed by name or not.

I do get it though.I know why I need to do it the JS or Java or "blah" way, but this is why people gravitate to things like python. If I had to choose between JS "classes" and Python
"classes", I would pick Python. At least Python makes classes separate from datatypes clearly. I shouldn't choose between having a "new" statement when all an "object" or "class" has is properties like a damn vector. Make a struct a damn struct ir GTFO. And god forbid I separate OOP classes in JS. But that's not what JS is for that eh? So why make it viable for the canvas tag???

I'm just bitching though... It's not doing what I want it to do with how I'm used to, so I bitch. JS isn't meant to do game dev and never was. But the only cross-platform things I can do with mobile is HTML5/Javascript, Flash, or native dev. Flash is dead or is dying, so what am I left with? HTML5 IS the future for mobile and web, albeit mobile or not, shit. Adobe already knows this don't they, which is why phonegap is Adobe. Native webviews, yeah, lets laugh and make more excuses.

BTW, phonegap cloud is a fucking rip-off. And so is the Flash cross-compiler....

Again, just bitching about JS.
>>>import this
-----------------------------

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Javascript Array Remove Object
« Reply #1 on: February 17, 2014, 07:31:21 pm »
JavaScript has historically been very clunky and poorly designed, especially its type system. It wasn't until the advent of jQuery in 2006 that JS began experiencing a major surge and started developing an ecosystem.

But anyway, this is why most web developers rarely work with vanilla JS and use some library like jQuery or underscore.js, among many others. They all provide convenient and (important) cross-browser compatible abstractions. Certain methods like indexOf, often used in vanilla JS to traverse an array, have bad IE support, for instance.

Here's a primer on associative arrays (called objects in JS): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

There are no conventional "classes" in JavaScript, there's prototypes.

However, the ECMAScript 6 standard intends on fixing lots of major smells and kludges, including adding classes and iterators, among many other things. Most major browsers have already implemented a large chunk of features, although there's still time until it's standardized. There's a nice walkthrough of ES6 features here: code.tutsplus.com/articles/use-ecmascript-6-today--net-31582. You can look up more.

However, in the meantime, to make things more tolerable... have you heard of CoffeeScript? It's a dynamic language inspired by Python and Ruby that compiles to JavaScript and makes JS much cleaner and easy to work with. Essentially syntactic sugar on top of JS. Among its features are classes and list comprehensions.

It's been around since 2009 and it's used by major companies like GitHub and Dropbox, so it's stable. GitHub even explicitly recommends it.

http://coffeescript.org/

It might help soothe the pain.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Javascript Array Remove Object
« Reply #2 on: February 17, 2014, 08:05:24 pm »
Thank you Vezzy, I now have CoffeScript bookmarked, and will experiment with it on what I am trying to do. And other such things.

What I am trying to do is write something with the canvas tag, impliment the game in JS and "cross compile" it to work on the devices I need it to. What I wanted to do was have a separate file/class like all the other paridigms have told me for things like the "player" and the "enemy" and abstract to the files of their own and such. I find it hard, it could be cause I don't know JS well enough, but I rant because of it. With something like python or C/++ or other languages, I can make a file/class and "import" or #include it, then inherit from there. I'm sure vanilla JS has something similar, but searches and such have turned up meh. And things that have turned up are obscure from what I am used to in languages that I know.

And the Array/List/Dict/Tuple/container containing other objects is just me bitching. I'm not opposing vanilla or of the such, it's just not what I'm used to. I will do it, have done it, and will continue doing it in the future. Either with languages I don't understand or even my wife that isn't a language, but I still don't understand. I just bitching I guess, and your input on thins and other threads people thing I don't read but I do and see your input, it's wonderful.

I'm trying to dev for mobile, cross-platform, one set of code for IOS and Android. Honestly, I don't know if my bitching is fruitful cause I haven't tested on an android device yet. I don't even know if the webview can do what I ask it to do, or even if the framerate can handle the mainloop.  (I have a feeling it can though, even without webGL. I'm not trying to rape anything here lulz).

Just me bitching like the Nazies when the Jews didn't do what I told them to. But even the Nazies learned and experimented and found what worked eh?

Agreed, this should have gone in the Bitch-and-Whine section, but you gave input pertaining to the scripting side of things. But a fruitless google or duckduckgo search from a CS student might bring light on the subject, so it stays here.

When I get to it, and make it happen in code, I'll post it here for the CS students. Hopefully the right way of doing it on what needs to be done. Might take a 24 hours or so but it shall be done.
>>>import this
-----------------------------

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Javascript Array Remove Object
« Reply #3 on: February 17, 2014, 08:30:51 pm »
JavaScript has no native import/include statement, or anything of the sort. Rather, you simply embed files with the <script> tag in sequential order, or use some alternative methods that are questionable: https://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file

It's simply how the language was designed. JavaScript was never meant to be what it ultimately did become, and it has a lot of warts. The ecosystem is also very stratified, because right now we're basically in the Wild West stages of JavaScript. But things are stabilizing.

As for making HTML5/JavaScript games, they should work on any device with a browser. That's kind of the beauty of it. Although if I understand, you want it to be contained within an app? Otherwise you can simply make the game into an web application (perhaps using a game framework like Phaser, it's popular) and then the iOS/Android "app" itself is just a wrapper that launches you into the website.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet