function bar() {
console.log(this);
}
bar();
var foo = {};
function bar() {
console.log(this === foo);
}
foo.bar = bar;
foo.bar();
var foo = {
bar: 123,
bas: function () {
console.log('inside this.bar is:', this.bar);
}
}
foo.bas();
function Foo() { }
console.log(Foo.prototype);
console.log(Foo.prototype.constructor === Foo);
var foo = {
__proto__: {
__proto__: {
__proto__: {
bar: 123
}
}
}
};
console.log(foo.bar);
function Foo() { }
var foo = new Foo();
console.log(foo.__proto__ === Foo.prototype);
function Foo() {}
Foo.prototype.log = function() {
console.log('log');
}
var a = new Foo();
var b = new Foo();
a.log();
b.log();
function Foo() {
this.bar = 123;
console.log('Is this global?: ', this == window);
}
// without the new operator
Foo();
console.log(window.bar);
// with the new operator
var newFoo = new Foo();
console.log(newFoo.bar);
function Foo(val) {
this.val = val;
}
Foo.prototype.log = function() {
console.log(this.val);
}
var a = new Foo(1);
var b = new Foo(2);
a.log();
b.log();
var foo = {
bar: 123,
bas: function () {
console.log('inside this.bar is:', this.bar);
}
}
var bas = foo.bas;
bas();
var foo = {
bar: 123,
bas: function () {
console.log('inside this.bar is:', this.bar);
}
}
var bas = foo.bas.bind(foo);
bas();
var foo = {
bar: 123,
};
function bas() {
console.log('inside this.bar is:', this.bar);
}
bas.call(foo);
var foo = {
bar: 123,
};
function bas(a, b) {
console.log('inside: this.bar(%d), a(%d), b(%d)', this.bar, a, b);
}
bas.call(foo, 1, 2);
function Animal() { }
Animal.prototype.walk = function () { console.log('walk') };
function Bird() { }
Bird.prototype.__proto__ = Animal.prototype;
Bird.prototype.fly = function () { console.log('fly') };
var animal = new Animal();
animal.walk();
var bird = new Bird();
bird.walk();
bird.fly();