Last active
January 5, 2016 06:38
-
-
Save lgh06/fb1e1cfc2f43f0d835ea to your computer and use it in GitHub Desktop.
simple code for learning call and apply
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 Product(name, price) { | |
this.name = name; | |
this.price = price; | |
} | |
function Food(name, price) { | |
Product.call(this, name, price); | |
this.category = 'food'; | |
} | |
function Toy(name, price) { | |
Product.call(this, name, price); | |
this.category = 'toy'; | |
} | |
var cheese = new Food('feta', 5); | |
var fun = new Toy('robot', 40); | |
console.log(cheese); | |
console.log(fun); | |
function f(a,b,c,d){ | |
console.log(this.x); | |
console.log(arguments); | |
console.log(a); | |
console.log(typeof arguments) | |
} | |
var obj = {x:4}; | |
f.call(obj,11,22,33,44,55); | |
f.apply(obj,[55,44,33,22,11]); | |
//below is OUTPUT in Node 0.10.38 | |
Food { name: 'feta', price: 5, category: 'food' } | |
Toy { name: 'robot', price: 40, category: 'toy' } | |
4 | |
{ '0': 11, '1': 22, '2': 33, '3': 44, '4': 55 } | |
11 | |
object | |
4 | |
{ '0': 55, '1': 44, '2': 33, '3': 22, '4': 11 } | |
55 | |
object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment