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
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; | |
}, |
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
• 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) |
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
// Via Object Literal | |
var obj = { | |
get foo() { | |
return 'getter'; | |
}, | |
set foo(value) { | |
console.log('setter: ' + value); | |
} |
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
<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'); |
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
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; | |
} |
NewerOlder