Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
shaunwallace / gist:f1e24d2096166dfad4b4
Created November 19, 2014 11:54
Converting mistakes in errors - Strict Mode
// Assignment to a non-writable property
var obj1 = {};
Object.defineProperty(obj1, "x", { value: 42, writable: false });
obj1.x = 9; // throws a TypeError
// Assignment to a getter-only property
var obj2 = { get x() { return 17; } };
obj2.x = 5; // throws a TypeError
// Assignment to a new property on a non-extensible object
@shaunwallace
shaunwallace / gist:6b56f49ba42e786caf22
Last active August 29, 2015 14:09
Classical Inheritance
// constructor function to create the parent
function Parent( name ){
this.name = name || 'Bob';
}
// extending the parent
Parent.prototype.sayHi = function() {
return 'Hi from ' + this.name;
}
// empty child constructor function
@shaunwallace
shaunwallace / gist:6ecbaf1e4167a6b538ae
Created November 14, 2014 11:48
Module Pattern Definition
var APP = (function() {
// private members
var foo = true;
var bar = false;
// private method
function baz() {
return 1;
}
var PublicShape = function() {
// public members
this.width = 50;
this.height = 50;
};
PublicShape.prototype.getWidth = function() {
// public method with priveledged access to the private members
return this.width;
}
@shaunwallace
shaunwallace / gist:f396c3b407361b395952
Last active August 29, 2015 14:09
Simple Namespacing Method
var MYAPP = MYAPP || {};
MYAPP.namespace = function (ns_string) {
var parts = ns_string.split('.'),
parent = MYAPP, i;
// strip redundant leading global
if( parts[0] === "MYAPP" ) {
parts = parts.slice(1);
@shaunwallace
shaunwallace / gist:16418f9b255d448d9b34
Created November 12, 2014 13:00
Scripting inside HTML imports
// the scripts inside of the import reference it's containing document
var import = document.currentScript.ownerDocument;
// the main document references itself
var doc = document;
// now we can grab things we need from the imported document and clone them into the main document
// here we are grabbing the styles from the imported document and adding them to the main page
var styles = import.querySelector('link[rel="stylesheet"]');
doc.head.appendChild(styles.cloneNode(true));
@shaunwallace
shaunwallace / gist:ac115fbbffeb5bb509b7
Last active August 29, 2015 14:09
Custom Elements - using templates & shadow DOM
<template id="x-template">
<style>
h1 { color: #bada55; }
</style>
<h1>What you are reading came from a template and is part of a custom elements shadow DOM</h1>
</template>
var proto = document.create(HTMLElement.prototype, {
proto.createdCallback : {
value : function() {
@shaunwallace
shaunwallace / gist:aaa0944f2d74e547d2f1
Created November 12, 2014 11:28
Custom Element - Defining Properties and Methods
// example 1
var XElementProto = object.create(HTMLElement.prototype);
// add a method
XElementProto.foo = function() {
console.log('foo method called');
}
// add a read-only property
Object.defineProperty(XElementProto, "bar", {isCustomElement : true});
// register your custom element
@shaunwallace
shaunwallace / gist:c38399d9807d4ea85431
Last active August 29, 2015 14:09
Simple Shadow DOM example with <content> and <template>
<section class="info-card">
John Smith
</section>
<template class="info-car-tmpl">
<h1>Hello, <content></content>!</h1>
</template>
<script>
// define our shadow host
@shaunwallace
shaunwallace / gist:df3995f4ab181a4e5160
Last active August 29, 2015 14:09
Simple Shadow Tree Example
<section class="contact-card">Some default content</section>
<script>
// in order to create a shadow tree we have to first specify which element will be the shadow host
// once this has been done then we can add content to the host and the first node added becomes the
// shadow root and all other nodes will decend from this root.
var host = document.querySelector('.contact-card');
var root = host.createShadowRoot();
root.textContent = "Some content that will be inserted inside the shadow root.";