this that selves

Posted by moodyharsh on 2016-06-30

“How can different things look the same ?
I don’t know man, I didn’t do it.”

In OOP, self-reference of the Object is achieved with a
special keyword, typically this or self.

C++/Java/Ruby/PHP

this resolves to the current object and the parent object.
Like, this.parent or super( this ) or $self->SUPER::.

Multiple Inheritance Or Which This is That

With multiple parents, this can refer to the wrong parent.

Single inheritance is preferred for this reason.

This is also called the diamond problem.

C3 solves this problem by taking into the account the ordering
of the parents.

Python/Perl

Perl’s object model is inspired from python.

Roughly put, Methods == Functions, with a special first argument.

In perl you bless $stuff and in python you have,

1
2
class Foo(object):
pass

this’s confusing !

In javascript, every function has a “this”.

This means you sometimes do,

1
2
3
4
5
6
7
8
function foo() {
var that = this;
// or var self = this;
function bar() {
that.doSomething();
}
}

With callback functions, this becomes useful.

It’s painful to write an extra declaration, just like in
perl or python.

So I present to you selves, which makes writing declarations
obligatory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<a href="https://github.com/harsha-mudi/yx/blob/master/index.js">var yx = require('yx');</a>
function foo() {
var selfs = new yx.__selves();
selfs(this);
selfs().i.youShoudHaveDoneThat = function () {
return "I Told You So!";
}
this.bar = function () {
selfs(this);
test.equal(selfs().previous.youShoudHaveDoneThat(), "I Told You So!");
this.doSomething = function () {
console.log("But but ... !");
}
}
}

You should really be using the => from coffeescript.
Another approach is,

1
2
3
4
5
6
function foo() {
function bar(x,y) {
//...
}.bind(this);
}

You can install [yx] with npm install yx.
yx provides coffeescript’s internal methods like __extends for use in js.

::Credits

#perl and #node.js from freednode.