what's going on that made this necessary?
Relative to this system, what is changing>
// start with at string | |
var s = "my string"; | |
//change its value (remember this changing of value is by value not reference) | |
s.toUpperCase(); | |
// assign it to t | |
var t = s; |
//this foo function is really saying, | |
//"Hey, I am expecting about 3 arguments but give me what you can" | |
function foo(a, b, c){ | |
console.log(a,b,c); | |
} | |
//lets ask foo to take more and less parameters than expected | |
foo("anArg"); // logs in console => anArg undefined undefined |
// test gist http://jsfiddle.net/fJPkr/1/ | |
var handler = {}; | |
(function(h){ | |
//holds the topics | |
var topics = {}, | |
subUid = 0; | |
h.publish = function(topic, args){ | |
//only publish topics that were subscribed to |
/* | |
The different ways to create an object | |
*/ | |
/* | |
Example object needs to have this heirachy | |
/* | |
Notes from Eric Elliott's talk at the fluent conference | |
JavaScript has FREEDOM. | |
In classical inheritance you have tight coupling brought on by the parent-child relationship. This is good in some areas but as the project grows, making changes to the parent that ripple through the descendants can be a big burden and error/bug prone. Thus, the need for duplication by necessity. This is the situation where you have a class that does almost everything you want but it needs to do something a little differently so you create a very identical class for your new need. This is caused by the gorilla banana problem. Where you want a banana but what you got was a gorilla holding the banana and the entire jungle. The problem with object-oriented languages is they've got all this implicit environment that they carry around with them. | |
JavaScript has inheritance that does not have these problems. | |
"Program to an interface, not an implementation." | |
"Favor object composition over class inheritance." |
foo(1); | |
function foo(a){ | |
var b = 2; | |
bar(3); | |
function bar(c){ | |
//all parent variables are visible and editable from here | |
console.log(a); |
//start with a string literal | |
var a = "Hello, World"; | |
//add a new property to that string | |
a.dateCreated = "6-4-2013"; | |
//try to access that property | |
var b = a.dateCreated; | |
//b is still undefined |
//version without passing in parameters that are not available in the function scope | |
;(function(){ | |
//code here | |
//why the leading semi colon? | |
// this is a safe way to keep from having conflicts with other code that uses the same | |
// self invoking functions but left off the colon | |
})(); |
A Pen by sean-roberts on CodePen.