Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example 1
- var counter = {
- val: 0, // property 'val'
- increment: function() {
- // method 'increment' in object 'counter'
- this.val += 1; // counter.val += 1;
- }
- };
- counter.increment();
- console.log(counter.val); // 1
- counter['increment']();
- console.log(counter.val); // 2
- // Example 2
- var obj = {
- fn: function(a,b) {
- // method 'fn' in object 'obj'
- return this;
- }
- };
- var obj2 = {
- method: obj.fn
- };
- console.log(obj.fn() === obj); // true
- console.log(obj2.method() === obj2); // true
- /*
- var obj2 = {
- method: obj.function(a,b) {
- return this;
- }
- };
- */
Add Comment
Please, Sign In to add comment