Last active
December 11, 2017 09:19
-
-
Save mcustiel/078fc61fe40fbb613dcdebc58fdb9775 to your computer and use it in GitHub Desktop.
Learning proper JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mcustiel = mcustiel || { | |
isNamespace: function () { | |
return true; | |
}, | |
Namespace: (function (root) { | |
function NamespaceConstructor (root) { | |
var isNamespace = function (object) { | |
return object.isNamespace && object.isNamespace() | |
}, | |
getParentNamespace = function (namespacePathParts) { | |
var currentLocation = root; | |
for (var i = 0; i < namespacePathParts.length - 1; i++) { | |
if (typeof(currentLocation[namespacePathParts[i]]) === 'undefined') { | |
currentLocation[namespacePathParts[i]] = { | |
isNamespace: function () { | |
return true; | |
} | |
}; | |
} else if (!isNamespace(currentLocation[namespacePathParts[i]])) { | |
throw 'Overwritting an object'; | |
} | |
currentLocation = currentLocation[namespacePathParts[i]]; | |
} | |
return currentLocation; | |
}; | |
this.create = function (fullCannonicalName, constructor) { | |
var namespacePathParts = fullCannonicalName.split('.'), | |
parent = getParentNamespace.call(this, namespacePathParts), | |
className = namespacePathParts[namespacePathParts.length-1]; | |
if (parent[className] && isNamespace.call(this, parent[className])) { | |
throw 'Trying to overwrite a namespace with an object'; | |
} | |
parent[className] = new constructor(); | |
}; | |
} | |
return new NamespaceConstructor(root); | |
})(window) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyClassParent = (function() { | |
function ClassConstructor(constructorArgument) { | |
var initValue = constructorArgument; | |
this.parentMethod = function() { | |
console.log('I am the parent method. My initializer is: ' + initValue); | |
}; | |
}; | |
return ClassConstructor; | |
})(); | |
var MyClass = (function() { | |
var myStaticPrivateProperty = 0, | |
parentArgument = null; | |
ClassConstructor = function (constructorArgument) { | |
parentArgument = constructorArgument; | |
var myInstancePrivateProperty = constructorArgument, | |
myInstancePrivateFunction = function () { | |
console.log('I am private'); | |
}; | |
this.getMyInstancePrivateProperty = function() { | |
return myInstancePrivateProperty; | |
}; | |
this.incrementStaticPrivateProperty = function () { | |
myStaticPrivateProperty++; | |
}; | |
this.getStaticPrivateProperty = function () { | |
return myStaticPrivateProperty; | |
}; | |
this.callPrivateMethodInternally = function () { | |
myInstancePrivateFunction.call(this); | |
}; | |
// "Inheritance" | |
this.__proto__ = new MyClassParent(parentArgument); | |
}; | |
return ClassConstructor; | |
})(); | |
var myClassInstance1 = new MyClass('potato'); | |
var myClassInstance2 = new MyClass('tomato'); | |
console.log(myClassInstance1.getMyInstancePrivateProperty()); | |
console.log(myClassInstance1.getStaticPrivateProperty()); | |
myClassInstance1.incrementStaticPrivateProperty(); | |
console.log(myClassInstance1.getStaticPrivateProperty()); | |
console.log(myClassInstance2.getMyInstancePrivateProperty()); | |
console.log(myClassInstance2.getStaticPrivateProperty()); | |
myClassInstance2.incrementStaticPrivateProperty(); | |
console.log(myClassInstance2.getStaticPrivateProperty()); | |
myClassInstance1.parentMethod(); | |
myClassInstance1.callPrivateMethodInternally(); | |
myClassInstance1.myInstancePrivateFunction(); // This will throw an error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment