Skip to content

Instantly share code, notes, and snippets.

View brandonvanha's full-sized avatar
🏠
Hello

Brandon Ha brandonvanha

🏠
Hello
  • Toronto, Canada
View GitHub Profile
@brandonvanha
brandonvanha / checkSheet.js
Last active August 29, 2015 14:08
Cheat Sheet: Working with Objects
Cheat Sheet: Working with Objects
This section is a quick reference with pointers to more thorough explanations.
• Object literals (see “Object Literals” on page 198):
var jane = {
name: 'Jane',
'not an identifier': 123,
describe: function () { // method
return 'Person named '+this.name;
},
@brandonvanha
brandonvanha / EfficientCodes.txt
Last active August 29, 2015 14:08
Efficient Codes
• Apply the array method map() to a string:
> [].map.call('abc', function (x) { return x.toUpperCase() })
[ 'A', 'B', 'C' ]
Using map() generically is more efficient than using split(''), which creates an intermediate array:
> 'abc'.split('').map(function (x) { return x.toUpperCase() })
[ 'A', 'B', 'C' ]
Apply a string method to nonstrings. toUpperCase() converts the receiver to a string and uppercases the result:
> String.prototype.toUpperCase.call(true)
@brandonvanha
brandonvanha / settingAndGetting.js
Last active August 29, 2015 14:07
Defining Accessors via an Object Literal & Property Descriptors
// Via Object Literal
var obj = {
get foo() {
return 'getter';
},
set foo(value) {
console.log('setter: ' + value);
}
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/Javascript">
$(document).ready(function () {
$(function () {
$('.add').on('click',function(){
var $qty=$(this).closest('p').find('.qty');
@brandonvanha
brandonvanha / getAllPropertyNames.js
Last active August 29, 2015 14:07
Iterates over all properties (not just enumerable ones).
function getAllPropertyNames(obj) {
var result = [];
while (obj) {
// Add the own property names of `obj` to `result`
Array.prototype.push.apply(result, Object.getOwnPropertyNames(obj));
obj = Object.getPrototypeOf(obj);
}
return result;
}