Skip to content

Instantly share code, notes, and snippets.

@facundovictor
Last active July 21, 2016 20:11
Show Gist options
  • Save facundovictor/dbf3ad61bb5e6e9ac07ea348b056d4a5 to your computer and use it in GitHub Desktop.
Save facundovictor/dbf3ad61bb5e6e9ac07ea348b056d4a5 to your computer and use it in GitHub Desktop.
This is a practical resume of some JavaScript specifications
/*
@author: Facundo Victor <[email protected]>
_____________________________________________
Using Prototype
__proto__ is the actual object that is used in the lookup chain to resolve
methods, etc. prototype is the object that is used to build __proto__ when
you create an object with new:
*/
(( new Foo ).__proto__ === Foo.prototype);
(( new Foo ).prototype === undefined);
Object.hasOwnProperty(obj, prop); // Not inherited prop only
Object.getOwnPropertyNames(obj); // Retrieve all not inherited props
(prop in obj); // Check if it has inherited and not inherited prop, enumerable and not enumerables
// WARNING, do not confuse it with con for..in, that it's only for enumerables (inherited and not inherited props).
for (prop in Object.key(obj)){ _;} // Not inherited and enumerables only
Object.propertyIsEnumerable(obj, prop); // Not inherited and enumerables only
// create an object with null as prototype
Object.create( prototype );
obj = Object.create(null);
obj = Object.create({}); // equivalent to new Object()
obj = Object.create({}, {
p: {
value: 42,
writable: true, // Default is false
enumerable: true, // Default is false
// The configurable attribute controls whether the property can be deleted from the object and whether its attributes (other than writable) can be changed
configurable: true // Default is false
},
foo: { // value cant be defined when defining setter
configurable: false,
// Accessor properties (getters and setters)
get: function() { return 10; },
set: function(value) { console.log('Setting `o.bar` to', value); }
}
});
Object.defineProperties(obj, {
p: {
value: 42,
writable: true, // Default is false
enumerable: true, // Default is false
configurable: true // Default is false
}
});
Object.getOwnPropertyDescriptor(obj, 'p'); // Returns the config, but only for not inherited objects.
var obj = Object.create({ foo: 1 }, { // foo es una propiedad heredada.
bar: {
value: 2 // bar es una propiedad no enumberable.
},
baz: {
value: 3,
enumerable: true // baz es una propiedad propia enumerable.
}
});
var copy = Object.assign({}, obj);
console.log(copy); // { baz: 3 }
obj = {
foo: 1,
get bar() {
return 2; // bar wont be editable.
}
};
copy = Object.assign({}, obj);
console.log(copy); // { foo: 1, bar:2 } // bar will be editable.
Object.freeze(obj); // the object is made effectively immutable: prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed. In 'strict mode', it will throw a TypeError. Once an object has been frozen it cannot be unfrozen. Note that values that are objects can still be modified, unless they are also frozen. To make obj fully immutable, freeze each object in obj.
Object.isFrozen(obj);
Object.preventExtensions(obj); // new properties can't be added to it, and prevent adding to __proto__, but not to prototype. Don't prevent modifications or re-configuration.
Object.isExtensible(obj);
Object.seal(obj); // prevents new properties from being added and marks all existing properties as non-configurable. Attempting to delete or add properties to a sealed object, or to convert a data property to accessor or vice versa, will fail.
Object.isSealed();
// Scope non trivial notes:
var this_is_in_the_scope = true;
// Oops we forgot a var
this_is_in_global_scope = true; // global scope: 'window' object if you're in the browser, 'global' object if you're in nodejs.
/* MORE INFORMATION ***********************************************************
Type-conversion:
Because of the operator overloading, you can use some operator for
type-conversions, or the constructors. → http://jibbering.com/faq/notes/type-conversion/
ECMAScript 6 reserved keywords → https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
Void operator → Void operator
Javascript Proxy → https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Proxy
More awesome links:
- https://howtonode.org/object-graphs
- https://howtonode.org/object-graphs-2
- https://howtonode.org/object-graphs-3
- https://gist.github.com/Jxck/1073547
- http://bonsaiden.github.io/JavaScript-Garden/
Bitwise operators → https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Signed_32-bit_integers
******************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment