« first day (179 days earlier)      last day (28 days later) » 

3:00 AM
o7
 
oi m8
 
okay now, what's your issue
 
Well not so much issue as much as questions
I've seen here and there people talk about creating objects in JS. And I mean just vanilla, no special libraries. I thought JS was functional mostly, so I was curious what the reality of it is
 
You can create an object specifying it's prototypes
 
You have a simple example we can work with?
 
3:03 AM
creating an object:
var object = {}
 
If I may add something to that... is ES6 you can now create classes
 
no shit?
 
@AlexL Classes are just syntactic sugar around the prototype chain
Though it is worth knowing nonetheless
Creating an object from a prototype (ES5):
 
var prototypes = {
    "toString": function(){
        return Object.keys(this).join(",");
    }
}
Object.create(prototypes)
 
3:04 AM
var prototype = {
  foo: function() {
    console.log('hello, world')
  }
}
var obj = Object.create(prototype)

obj.foo()
> hello, world
Equivalent code in ES6..
class MyClass {
  foo() {
    console.log('hello, world')
  }
}
var obj = new MyClass()
obj.foo()
 
It's kind of like how you modify .Equals in C#
 
But what's inside the var is a function, how does that differ from a function?
 
no difference
prototypes are functions
 
@Quill sorry m8 I don't C# :)
 
@Quill wot?
prototypes are Just Objects (tm)
 
3:06 AM
they're like an object of functions
 
Everything in Javascript is Just An Object
 
well they are
 
They don't have to be functions, they can also be properties
And prototypes can also have prototypes of their own
 
well yeah
it's basically just like:
 
@AlexL My god that page offers some bad practices
 
3:06 AM
To be fair there is not a huge amount of difference between a function and a method, when it comes down to it
 
yeah, pretty much
add ES6's new syntax to the mix
and you have similar looking languages
 
@DanPantry It's literally the documentation, what part do you think is awful? just curious
 
@AlexL MDN docs are written by volunteers
 
Does var prototype have a special meaning, or could it be named anything at all?
 
@AlexL var Person = function () {}; ( why would you not name that function?); var MYAPP = MYAPP || {}; (wtf why?)
@Phrancis it can be named anything
 
3:08 AM
MYAPP.event = {
    addListener
 
Is it not the official docs though?
 
use prototypes
 
@AlexL No. It's community sourced.
 
Where are the official docs?
I must be missing something here
 
There are no 'official docs' for JavaScript, only the W3C standards and the implementations of them by vendors.
 
3:08 AM
@Phrancis prototype by itself is fine, but as a property of a variable it's locked
 
That's what I thought, and since Mozilla stepped forward to create a community reviewed site for docs, I've always just accepted them :P
 
@Phrancis Like I said, prototypes are Just Objects. Up until you use Object.create on them, they act like objects. As such, you can name them like any other object.
@AlexL The docs on their methods and properties are pretty damn good. But that tutorial in particular is pretty crap.
 
You should contribute :)
 
@AlexL I should do a lot of things.. ;-;
 
I'm gonna step out for a smoke but I want to learn more. You guys keep arguing about the documentation or something til I get back lol
 
3:10 AM
@Phrancis Lol we're not arguing :P I'll be here when you get back, just need to run to the shop to get a drink because I have my life in order
 
Haha sorry we got jumbled into your discussion
 
Yeah, I've got hours left at work so I'll be online
 
I walked into the SO Javascript chat for a few minutes the other day and wasn't impressed at all
 
Yeah, lot of help vampires
Anything in particular @AlexL?
 
It was silent, which I wasn't expecting, and when I scrolled up through the last few hours of chat it was all off topic and everyone talked like a 10 year old. Maybe I just had bad timing
 
3:21 AM
I was in their for a few days once
there was next to no decent conversation
most of the regulars stopped chatting when help vampires or people with questions came along
 
Sigh
anyways, care to review my review?
0
A: Multiple inheritance pattern for vehicle information

Alex LVery interesting concept, I could see this turning into a "Pimp my Ride" game. A few things before this is sent off to gamers: OOP It seems like you've kind of misunderstood what OOP is about. As it is, you have a lot of variables (not objects) which then are smushed together into a final conce...

I'm trying to take down some of these JS zombies and I wanna make sure I'm not misleading others
 
I don't think you can overload it like that
I think a setter with optional arguments should work
 
Shooting from the hip, how much (%) would you estimate the market uses/supports ES6?
 
Very little
5% maybe
You've got to compile your code down to ES5 before you can run it
Edge supports some ES6 syntax and no other browsers do
The big companies use it, as Babel.JS' homepage will tell you
but of course they use every technology the day after it comes out
 
OK in that case, assuming this ES
21 mins ago, by Dan Pantry
var prototype = {
  foo: function() {
    console.log('hello, world')
  }
}
var obj = Object.create(prototype)

obj.foo()
> hello, world
 
3:26 AM
that's ES5
 
Are things like void, static, private, public... supported?
 
No, those are ES6 keywords
 
I'm guessing the clever JS devs have figured out workarounds maybe?
 
ES6 looks a lot like Java to me
(which is a good thing I think)
 
3:27 AM
yeah, babeljs converts ES6 down to ES5, effectively performing the same
It may be a good thing to you, but it's a major syntax change that will stir the JavaScript community
 
No doubt
 
the old syntax will still be around though
so who knows, I guess
 
Not terribly interested in the controversy right now though, I'm sure I will at some point
 
void: return types aren't things
static: functions are default static, unless they use internal properties
public: well, kinda
private: you can use advanced closures to limit access
 
That's rough, but OK
 
3:30 AM
prototypes are absolutely the opposite of static, but some functions use internal properties that are kinda both
 
So if you don't return something, it's void by default eh?
 
I suppose so
the value of the function would be undefined
 
Close enough, I suppose
 
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
 
What about object variables or properties? Are those just var inside the object code?
 
3:32 AM
Babel.JS has some serious logic
 
back, did I miss anything
 
not really
 
^
 
@Phrancis public/private are not supported, no
 
@Phrancis internal properties are accessed like Java (?) this.foo = foo
so static is a valid ES6 keyword
 
3:37 AM
Yep. It essentially compiles to this
function MyClass() {

}
MyClass.staticMember = function() {}
class MyClass {
  static staticMember() {}
}
 
OK cool
I think I get it
var addNums = {
    add: function(a,b) {
        return a + b;
    }
}
var obj = Object.create(addNums)
console.log(obj.add(2,3)); //5
console.log(obj.add(99999,null)); //99999
 
BTW there is no need to use prototypes all the time unless you want to use/simulate inheritance
That code you just linked would work just fine without Object.create
 
Is it "use" or "simulate"?
waits for "it depends"
 
It's technically simulate because prototypes != OOP inhertiance
 
The thing about that is that you can attach prototypes to other primitive types using .create
 
3:41 AM
(Which is why I dislike "OOP Javascript")
 
I would've figured JS devs would embrace the functional-ish style, instead of try to shoehorn OOP into it
(just an observation, nothing else)
 
@Phrancis Most do
most places you hear the words "OOP Javascript" are from enterprises or recruiters
Using OOP to describe js is quite funny because in js literally everything is an object
2
 
I kinda like functional personally, seems usually simpler
 
You couldn't get any more object-orientated than that
But people use OOP to denote 'classical' OOP like C#/Java
The functional stuff is a lot more flexible and easier to reason about
It encourages you to move away from storing state
and that makes stuff a LOT easier to test
 
For a long time I coded nothing but functional PHP
Two bad things mixed together
 
3:44 AM
But JS has to be objects because it normally runs inside a browser program?
 
(or, rather, it encourages you to limit state)
@Phrancis No, it's nothing to do with the fact it runs in a browser.
 
oh
 
I mean, if you tried hard enough, you could write C scripts in the browser.
 
Ew
 
these days lots of sites run on node and react
 
3:45 AM
The everything-is-an-object model is just part of javascript's programming model
A very valuable part, IMO. Having functions as objects is super useful
 
Is JS object-ism influenced much by HTML DOM and such?
 
I don't know. Possibly?
The W3C uses IDL specs to describe HTML elements
And those IDLs do quite closely resemble the JavaScript interfaces
So, maybe?
 
OK
 
Haha hey @Quill just peeked at your profile, I'm 17 too :P
 
I got one more question/request... if I may
 
3:47 AM
Go for it, @Phrancis.
 
@AlexL :-)
 
I was 17 once. it was terrible
 
I often see things in JS that take a function as an argument, and that seems a bit "odd" to me at least. Normally these blocks end with a }); ... From my understanding a function returns an argument (including void) but I just can't seem to place it in my head in a way that makes sense
Or is it )};?
 
> I often see things in JS that take a function as an argument
 
});
 
3:51 AM
This is called a higher-order function. It's a staple of functional programming.
You're thinking of something like this, right?
 
I think so
 
foo(function() { return doSomething() })
 
I tried to do JavaScript callbacks in C# earlier and it went horribly
 
or perhaps
foo(() => doSomething())
 
I only recall the first syntax @DanPantry
But yes, that
 
3:52 AM
@Quill Yeah, that's because callbacks are shitty. Use Task, which is C#'s promise.
LINQ in C# uses higher order functions correctly.
 
There's a lot wrong in that file and I don't know where to start :P @Quill
@Phrancis What's the confusing part?
 
I don't know the confusing part yet, it just seems so foreign
 
@Phrancis It's becoming more and more common
Java and C# both use it now
 
Like I couldn't write one of those that does anything useful (like add 2 numbers) to save my life
 
3:54 AM
@Phrancis no, but you could use them to provide a functor for iterating over an array, for example.
 
@Phrancis you don't need to add them in the parameters by default
 
function each(array, functor) {
  for(var i = 0; i < array.length; i++) {
    functor(array[i], i, array)
  }
}

each([1,2,3], console.log)
> 1
> 2
> 3
 
I felt forced to use tons of callbacks in my latest question
 
A better one would be the higher order map function..
function map(array, functor) {
  const newArray = []
  for(let i = 0; i < array.length; i++) {
    newArray[i] = functor(array[i])
  }
  return newArray
}
const newArray = map([1, 2, 3], a => a * 2)
console.log(newArray)
> 2
> 4
> 6
 
Presumably, functor is a different function?
 
3:57 AM
In each, functor is any function. It is passed 3 arguments; the object in the array, the index of that object, and the array itself.
 
var things = {
    house: houseFunc,
    floor: floorFunc,
    abc: abcFunc,
    foo: fooFunc
};
var thing = "abc";
things[thing]();
 
In map, functor is a function that takes an argument - which is an object. The return value of that function is then assigned to the same index as the originating objet in a new array, and the changed array is returned.
 
you might use that instead of a switch statement
 
This allows you to change a collection without modifying the original collection, which is handy.
javascript already has a map function, though.. Array.prototype.map, or [].map(..). It was just an example.
 
Is that way of doing things generally used in functional, e.g. instead of a for loop going over a map/array or whatever?
 
4:01 AM
it is generally preferable to use the functional alternatives yes
 
 let i = 0; i < array.length; i++
 
forEach isn't used much in my codebase because I prefer to use immutable functions and forEach promotes side effects
 
isn't let for constants?
 
I've never seen the word functor used, so it's a bit unfamiliar
 
const is for constants.
 
4:02 AM
meanwhile I really really like forEach
 
@Quill forEach is bad and you should feel bad. 8)
 
well let is constant syntax in Swift
 
The problem with forEach is that it encourages side effects
 
@DanPantry m e e e e e e h h h h h
 
let is the ES6 syntax for var AFAIK
 
4:02 AM
@Phrancis sort of
 
Well, better than var, but same idea
 
let is a block-scoped variable declaration which is mutable
it's basically a less shit var
 
LOL
79
A: LOL debugging are we? - Stack Overflow homepage alerts false

Matt ShermanThis one was my fault. UPDATE: Our 5 Why Assessment of the situation Problem: there is an alert on every page of Stack Overflow Why? Because Matt deployed a version of the Careers ad code with a debugging line in it Why? Because he forgot to remove it before pushing his change Why? Because...

 
lol
 
In summary, am I gathering that JS really should (often) be used like other FP (Lisp, F#, Haskell, etc.)? And was that FP already there all along and I just missed? Or do people just really suck at JS (in general)?
 
4:08 AM
@Phrancis FP has become more popular with libraries like lodash/underscore, ramda etc whicih have only really exploded into popularity since the advent of npm
Before that point it was mostly jQuery soup (because back in the dark ages, no standards existed, jQuery was necessary to maintain compatibility between IE and not-IE land)
 
There's also a lot of people that really suck at JavaScript
 
Yes, ^^^
 
lol
 
A lot of people don't view JavaScript as a "real" programming language (many CRitters included)
 
the whole no var necessary, no complexity in static, private, public
 
4:09 AM
As such they don't learn the nuances of it and such
And there's no doubt that JavaScript is the most popular (in terms of how much it is used) language in the world simply because it is on every single web page.
So you have a large pool of idiots
 
Bad JavaScript has more side effects than WebMD articles
 
But since ES5/ES6 and npm's existance, javascript has gotten a lot better
 
not to mention the massive amounts of tutorial pages
 
I've never met someone who has died of an illness from JS, but ok lol
 
often a lot of blog tutorial posts are horrible in quality and should feel bad for opening their blog
 
4:11 AM
It's not that blog tutorials are horrible, as such.
It's just that they rely heavily on jQuery as a crutch
a lot of people do
And that comes down to people viewing javascript as a toolkit rather than a language
 
and you get w3schools top of SEO
 
LOL^
 
Yeah, never pay attention to w3schools. MDN FTW
 
There's never a correct way to learn JS
 
w3schools is truly an awful resource, I have a plugin that makes sure it is removed from my search results
 
4:12 AM
W3S has half-reasonable SQL tutorial (the other half is broken because bad JavaScript)
 
no official source, so you get mismatched tutorials that teach you different things
 
It is a lot of fluff with not much useful info on at all, compared to MDN whicih has user curated standards docs
Javascript is basically the wild west of programming
If you do it right, though, it can be really elegant and neat and some of the most beautiful code you've written
 
No doubt^
 
If you do it wrong you end up with something that resembles an italian dish
I believe they call it "Mom's spaghetti"
knees weak arms are heavy
 
Had it going through my head before last post
 
truly the reason why the internet was created.
 
lol
I have to say, FP feels quite "wild" compared to OOP, but maybe it's all part of the fun of it
You guys are great, don't let anyone tell you otherwise.
 
lol you too there Phrancis
 
thanks @Phrancis, you too man :-)
 
I remember when you first joined CR :')
 
4:29 AM
also definitely good for JS programmers, or maybe programmers in general:
 
hooray :D
ding dong the witch is dead
 
About fLIcking time!
 
in JavaScript on Stack Overflow Chat, 1 min ago, by ajaxGuru
why don't they change "node" in the doc to "item" or "id"
in JavaScript on Stack Overflow Chat, 1 min ago, by Quill
because they're nodes not items
in JavaScript on Stack Overflow Chat, 1 min ago, by ajaxGuru
people at first think nodejs
;-;
 
4:46 AM
Sounds like maybe someone who doesn't know JSON or XML much to know what nodes are
 
5:03 AM
0
Q: Pomodoro Timer in Javascript

rfmonkeydevI've made a Pomodoro Timer application. All it does is countdown from a time in minutes given by the user to 0:00. It does this first for a session time, and then switches to a break timer and continuously loops like this until the application is paused or closed. The time can be adjusted by cli...

 
oh my
well this conversation in SO's JavaScript was one big facepalm
 
5:28 AM
The syntax I just linked in there is SUPER interesting, though
function sql(string, escapedArguments) {
  console.log(arguments)
}

const username = 'bar'
sql`SELECT * FROM accounts WHERE username=${username}`
sql will give you an array of strings which you can concatenate together
you concatenate them with the values of escapedArguments
so the first + second string in string would be concatenated with the first argument in escapedArguments
It's hard to explain
but basically in the example I gave
string will be ["SELECT * FROM accounts WHERE username=", ""] and escapedArguments will be { 1: 'bar' }
 
so how do you transform that into a query?
 
in that example, you would do something like
string[0] + escapeSomehow(escapedArguments['1']) + string[1]
 
right. that seems a little pointless
 
It isn't
 
what if you do multiple arguments
 
5:32 AM
Well, I gave a hard coded example lol
you could of course alter the code so it works with all numbers of args
 
5:55 AM
> me: can you provide a gist please?
*provides a completely unrelated gist*
me: can you provide one that's actually about the problem you want help with?
him: oh, you want an example
how does one
 
 
3 hours later…
8:29 AM
0
Q: Capital market dashboard page

overexchange Problem statement The Capital market exercise contains a dashboard page of account names, built using AngularJS. Solution /* AccountsController.js */ appRoot.controller('AccountsController', function($scope){ $scope.rows = [ { name: 'Brokerage Account 3', ...

 
 
3 hours later…
11:31 AM
-1
Q: How to optimize nested if?

LockHi I want to know is there a better way to do this? condition_a is more important than b, b is more important than c, etc. var slot = condition_a if (!slot) { slot = condition_b if (!slot) { slot = condition_c if (!slot) { slot = condition_d if (!slot) { slot = c...

 
 
3 hours later…
2:53 PM
0
Q: Nodejs Async get request

null05I made a small application that makes async calls to an api (local in this case). The target application will make a million of calls per day. With maxSockets set to 1 I had a time of 45 seconds. With maxSockets = 10, the execution time has dropped to 40 seconds. How can I make this code go fa...

 
3:16 PM
0
Q: angularJS - changing page language using a service

Miha ŠušteršičI'm rebuilding a basic webpage using AngularJS, and want to implement changing languages when clicking an icon. So far I've build a service called 'langService', that has functions to set and get language. I then pass the service a value (say 'eng' or 'slo') with the set language function when cl...

 
 
4 hours later…
7:22 PM
0
Q: Javascript extensive Promise chaining

bali182I recently started working with JS Promises. What I always like to do, is create function, which returns a Promise with the final desired result, but I only do 1 operation/then. Consider something like this (this fetches the versions of an npm package in an array in reverse order): function pack...

 
 
2 hours later…
8:57 PM
0
Q: XSS Sanitization of RSS Feeds using JS

Emmett R.I need to pull an rss feed with links, headlines, and descriptions onto the front page of a website, and while I would like to think the source is trustworthy, you can never rule out malicious actors. I also would like to avoid iframes, since a sufficiently secure iframe sandbox would impact the ...

 

« first day (179 days earlier)      last day (28 days later) »