Conversation started Dec 26, 2015 at 17:48.
Dec 26, 2015 17:48
@DanPantry:
It's the same as creating an extension method in C# -- you're extending the behaviour of a specific type with a specific functionality. The compatibility issue is moot: if a new function is introduced that does this for you then you just remove your own definition and that's that. Provide unit tests and you will know if a breaking change is introduced which you can act accordingly upon. — Jeroen Vannevel 2 mins ago
On prototyping.
Dec 26, 2015 18:19
That sounds about right, @Mast.
Except it is more like adding a instance method than an extension method.
Why is adding an instance method not an extension?
But basically, it's a good idea here and a bad idea somewhere else?
Now I just need to figure out the difference.
 
1 hour later…
Dec 26, 2015 19:35
@Mast I'm not sure what you're asking
@DanPantry If I understand you correctly you can add methods to a class in two ways, as instance method and as extension method.
@Mast in javascript?
To a class - no, you can only add methods to a class via a prototype.
you CAN add methods to an INSTANCE
but that is not to a class.
Ah, now I get it.
Here's the difference.
class Foo {
  instanceMethod() {}
  static classMethod() {}
}
This transpiles to this:
const Foo = (function() {
  function Foo() {}
  Foo.classMethod = function classMethod() {}
  Foo.prototype.instanceMethod = function instanceMethod() {}
  return Foo;
}());
now if I do new Foo() I'll get an instance of the Foo class. It will have instanceMethod on it - same as any other OOP language
Basically you're extending the class but only for that instance.
A kind of overloading.
Dec 26, 2015 19:40
Er, sort of. What I just showed you was prototypal inheritence.
the other one, the extending one, looks like this.
var foo = new Foo();
foo.instanceMethod = function instanceMethod() {}
This will override foo.instanceMethod for that specific instance.
Generally speaking, you don't want to do this for obvious reasons
So, do the first and not the latter?
if you're making classes, generally speaking you're sharing behaviour
So yes, use prototypes
If you're not sharing behaviour you probably shouldn't be using classes but object literals instead
Which are just the normal way of creating objects..
var foo = {
  function instanceMethod() {}
}
And not like I did here:
5
Q: Two-dimensional array, modified one field at a time

MastTitle says it all. I'm basically initializing a two-dimensional array and changing the fields one at a time. It might be a good idea to do this OO. A Grid class seems right, perhaps as a subclass of Array. So far I haven't been able to successfully port it. Pointers would probably be a good idea...

Array.shoot = function(row, column, array) {
  array[row][column] = "H";
  return array;
}
Yeah, that doesn't make sense at all
Well, I mean
those are essentially static utility methods
But uh
don't use the namespace Array either
You don't want to be modifying vendor types (like Array, String, Function) etc
for what it is worth, by the way
The latest tends in JS tend to avoid using classes and use more functional concepts instead
Although you definitely want to use classes if you'r ereusing functionality
because attaching things to the prototype chain reduces memory usage
(because functions on the prototype chain are only created once, whereas closures have to be created each time)
In this instance I would use Flambino's first implementation personally :)
@DanPantry Right. That's what I learned.
Don't do that.
But prototyping on Strings is perfectly ok?
@DanPantry Yea, that's what I'm thinking.
Dec 26, 2015 19:56
@Mast No.
Do not modify any vendor type
or their prototypes
@DanPantry String.prototype.endsWith
That's what is being suggested.
@Mast that's because endsWith, I believe, is a method introduced in ES6.
Modifying a vendor type in this case is okay. This is called a shim.
> Modifying a vendor type in this case is okay.
> Do not modify any vendor type
At least now I know why it's confusing.
Okay, i should clarify
Don't modify a vendor type wtih a custom method
endsWith is not a custom method, but it exists in ES6
for backwards cmpatibility purposes, we check whether or notendsWith exists. if it doesn't we shim it
Dec 26, 2015 20:21
Right.
 
Conversation ended Dec 26, 2015 at 20:21.