Skip to content

Instantly share code, notes, and snippets.

View 1kohei1's full-sized avatar

Kohei 1kohei1

  • Google
  • Santa Clara
View GitHub Profile
if (DEBUG) { console.log('Debugging is starting'); }
// Results the reference error when debug.js is not loaded.
if (typeof DEBUG !== 'undefined' && DEBUG) { console.log('Debuggins is starting'); }
// This is safer.
function a() {}
var b = function() {}
typeof a; 'function'
typeof b; 'function'
for (let c in b) { console.log( c ); }; // a
// Enumerable travels prototype chains.
for (let key in Object.keys( b )) { console.log( key ) }; // nothing prints out
var a = { a: 2, }
var b = Object.create( a );
console.log( b ); // {}
console.log( b.a ); // 2 <- It got 2 from b's prototype chain
/*
b with prototypes
{
__proto__: {
a: 2,
__proto__: { ... }
function mixin(sourceObj, targetObj) {
for (var key in sourceObj) {
if (!(key in targetObj)) {
targetObj[key] = sourceObj[key];
}
}
return targetObj;
}
var Vehicle = {
engines: 1,
class A {
public void ignition() {
System.out.println("A ignition");
}
public void drive() {
this.ignition();
System.out.println("A drive");
}
}
class B extends A {
var myObject = {
a: 2,
b: 3,
};
Object.defineProperty(myObject, Symbol.iterator, {
enumerable: false,
writable: false,
configurable: true,
value: function() {
var o = this;
var a = {};
Object.defineProperty(a, 'a', {
value: 1,
writable: true,
enumerable: true,
configurable: true,
});
Object.defineProperty(a, 'b', {
value: 2,
writable: true,
var a = {
a: 2,
};
("a" in a); // true
("b" in a); // false
a.hasOwnProperty("a"); // true
a.hasOwnProperty("b"); // false
// in operation also consults object prototype. So compare following two linces
var a = {
get a() {
return 2;
}
}
Object.defineProperty(a, "b", {
get: function() { return this.a * 2 },
enumerable: true,
});
a.a; // 2