Repository navigation
Expand file tree
/
Copy pathpre.html
More file actions
118 lines (84 loc) · 2.85 KB
/
Copy pathpre.html
File metadata and controls
118 lines (84 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>pre.md</title>
</head>
<body>
<p><link href="/css/markdown.css" rel="stylesheet"></link></p>
<p>And now for something completely different: the inheritance!</p>
<blockquote>
<p>Inheritance? Oh, come on! How much of it yet! Go and find a bunch of
that shit on the internet!</p>
</blockquote>
<p>We introduce singular, simple, profitable multiple inheritance system.
It's built entirely of prototypes (and not a single array lookup), and
it'll make your soul relaxed and your heart warm especially if you've heard
anything of Io or Lua or Self or whatever prototypal language
other than javascript. Enter the whole forest of a Class!</p>
<h2>Mushroom Inheritance</h2>
<h3>The Fore-Story</h3>
<p>We need an <code>__extend</code> function:</p>
<pre><code>function __extend(o,e){
for(var k in e){
o[k]=e[k]}
return o;
}
</code></pre>
<p>a <code>__clone</code> function:</p>
<pre><code>function __clone(o){
return {__proto__:o}
}
</code></pre>
<p>and pretty like nothing more.</p>
<p>Let's put them together and call the thing <code>Obj</code>:</p>
<pre><code>var Obj={
extend:function(e){
return __extend(this,e)},
clone:function(){
return __clone(this)},
}
</code></pre>
<p>Now we could create all kinds of things just like this:</p>
<pre><code>var Wheeled=Obj.clone().extend({
wheels:0,
go:function(){console.log("tag dag dag da")},
})
var Bike=Wheeled.clone().extend({
owner_name:"nobody",
go:function(){console.log("hey, "+this.owner_name+", din don!")},
init:function(owner){this.owner_name=owner;return this}
})
var b0=Bike.clone().init("Mike")
b0.go()
>> hey, Mike, din don!
</code></pre>
<p>To keep instances a bit different from classes let's do this way:</p>
<pre><code>function __delta(o){
return delta}
Obj.extend({
new:function(){
var d0=__delta(this)
d0.init && d0.init.apply(d0,arguments)
return d0}})
</code></pre>
<p>So now we can do the last bit of bike pirouette like this:</p>
<pre><code>var b0=Bike.new("Mike")
b0.go()
>> hey, Mike, din don!
</code></pre>
<p>That would suffice for any semi-complex task of javascript developer, and
that makes us content with terminology to be used in next article.</p>
<p>But to be even more happy, we'd spend another minute or two here.
What if we embed dictionaries in our nifty objects? Surely we
would have them embedded. And if we want them to be modifiable from leaves-instances,
but yet stay unchanged in class-instances, we could do like this:</p>
<pre><code>function __extend(o,e){
return o}
function __delta(o){
return __clone(o)}
</code></pre>
<p>So that's like it. And all the mushrooms <a href="/clos/">following</a>.</p>
</body>
</html>