I think you have some confusion about functions and object's prototype, I hope I will be able to clarify a bit.
First of all:
function foo_2 () {
console.log("I ma foo 2");
}
This is called "function declaration", where:
var foo_1 = function () {
console.log("I ma foo 1");
}
Is called "function expression". You can have detailed information here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression, with also the Function constructor example.
Since in javaScript every thing is an object, are two new objects created with name foo_1 and foo_2 in the previous code?
Yes, they're objects, and if you modify the prototype chains you will affect them:
Object.prototype.testName = "this is an experiment";
function test() {}
console.log(test.testName) // "this is an experiment"
But test.prototype is an object too, so if you do:
console.log(test.prototype.testName) // "this is an experiment"
The reason because you do not get the property correctly it's probably because is inherited, and the console you're using (node, browser) displays only the own properties of an object (like Object.keys), without crawl the whole prototype's chain.
That is the main reason because you shouldn't change the Object.prototype: you will affect mostly everything and you can't predict the consequences.
I said "mostly" because you can create objects that actually doesn't inherit from Object's prototype. You can do it using Object.create – or the non-standard where supported __proto__:
var foo = {};
console.log(foo.testName) // "this is an experiment"
var bar = Object.create(null);
console.log(foobar.testName) // undefined
Finally, I was wondering if the these function definitions are equivalent to the following code:
this.foo = function() {
console.log("I ma foo");
}
It depends by what is this when you execute this code. They're mostly equivalent if you execute the code in the global scope, so this would be the global object (in browsers is window). However, there is a difference. Using this syntax, you would be able to delete the foo property, where you can't do that when is declared. So:
this.foo = function() {};
var bar = function() {};
delete foo // can be deleted;
delete bar // shouldn't be deleted;
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
I think you have some confusion about functions and object's prototype, I hope I will be able to clarify a bit.
First of all:
function foo_2 () {
console.log("I ma foo 2");
}
This is called "function declaration", where:
var foo_1 = function () {
console.log("I ma foo 1");
}
Is called "function expression". You can have detailed information here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression, with also the Function constructor example.
Since in javaScript every thing is an object, are two new objects created with name foo_1 and foo_2 in the previous code?
Yes, they're objects, and if you modify the prototype chains you will affect them:
Object.prototype.testName = "this is an experiment";
function test() {}
console.log(test.testName) // "this is an experiment"
But test.prototype is an object too, so if you do:
console.log(test.prototype.testName) // "this is an experiment"
The reason because you do not get the property correctly it's probably because is inherited, and the console you're using (node, browser) displays only the own properties of an object (like Object.keys), without crawl the whole prototype's chain.
That is the main reason because you shouldn't change the Object.prototype: you will affect mostly everything and you can't predict the consequences.
I said "mostly" because you can create objects that actually doesn't inherit from Object's prototype. You can do it using Object.create – or the non-standard where supported __proto__:
var foo = {};
console.log(foo.testName) // "this is an experiment"
var bar = Object.create(null);
console.log(foo.testName) // undefined
Finally, I was wondering if the these function definitions are equivalent to the following code:
this.foo = function() {
console.log("I ma foo");
}
It depends by what is this when you execute this code. They're mostly equivalent if you execute the code in the global scope, so this would be the global object (in browsers is window). However, there is a difference. Using this syntax, you would be able to delete the foo property, where you can't do that when is declared. So:
this.foo = function() {};
var bar = function() {};
delete foo // can be deleted;
delete bar // shouldn't be deleted;
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
I think you have some confusion about functions and object's prototype, I hope I will be able to clarify a bit.
First of all:
function foo_2 () {
console.log("I ma foo 2");
}
This is called "function declaration", where:
var foo_1 = function () {
console.log("I ma foo 1");
}
Is called "function expression". You can have detailed information here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression, with also the Function constructor example.
Since in javaScript every thing is an object, are two new objects created with name foo_1 and foo_2 in the previous code?
Yes, they're objects, and if you modify the prototype chains you will affect them:
Object.prototype.testName = "this is an experiment";
function test() {}
console.log(test.testName) // "this is an experiment"
But test.prototype is an object too, so if you do:
console.log(test.prototype.testName) // "this is an experiment"
The reason because you do not get the property correctly it's probably because is inherited, and the console you're using (node, browser) displays only the own properties of an object (like Object.keys), without crawl the whole prototype's chain.
That is the main reason because you shouldn't change the Object.prototype: you will affect mostly everything and you can't predict the consequences.
I said "mostly" because you can create objects that actually doesn't inherit from Object's prototype. You can do it using Object.create – or the non-standard where supported __proto__:
var foo = {};
console.log(foo.testName) // "this is an experiment"
var bar = Object.create(null);
console.log(bar.testName) // undefined
Finally, I was wondering if the these function definitions are equivalent to the following code:
this.foo = function() {
console.log("I ma foo");
}
It depends by what is this when you execute this code. They're mostly equivalent if you execute the code in the global scope, so this would be the global object (in browsers is window). However, there is a difference. Using this syntax, you would be able to delete the foo property, where you can't do that when is declared. So:
this.foo = function() {};
var bar = function() {};
delete foo // can be deleted;
delete bar // shouldn't be deleted;
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
I think you have some confusion about functions and object's prototype, I hope I will be able to clarify a bit.
First of all:
function foo_2 () {
console.log("I ma foo 2");
}
This is called "function declaration", where:
var foo_1 = function () {
console.log("I ma foo 1");
}
Is called "function expression". You can have detailed information here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression, with also the Function constructor example.
Since in javaScript every thing is an object, are two new objects created with name foo_1 and foo_2 in the previous code?
Yes, they're objects, and if you modify the prototype chains you will affect them:
Object.prototype.testName = "this is an experiment";
function test() {}
console.log(test.testName) // "this is an experiment"
But test.prototype is an object too, so if you do:
console.log(test.prototype.testName) // "this is an experiment"
The reason because you do not get the property correctly it's probably because is inherited, and the console you're using (node, browser) displays only the own properties of an object (like Object.keys), without crawl the whole prototype's chain.
That is the main reason because you shouldn't change the Object.prototype: you will affect mostly everything and you can't predict the consequences.
I said "mostly" because you can create objects that actually doesn't inherit from Object's prototype. You can do it using Object.create – or the non-standard where supported __proto__ :
var foo = {};
console.log(foo.testName) // "this is an experiment"
var bar = Object.create(null);
console.log(foo.testName) // undefined
Finally, I was wondering if the these function definitions are equivalent to the following code:
this.foo = function() {
console.log("I ma foo");
}
It depends by what is this when you execute this code. They're mostly equivalent if you execute the code in the global scope, so this would be the global object (in browsers is window). However, there is a difference. Using this syntax, you would be able to delete the foo property, where you can't do that when is declared. So:
this.foo = function() {};
var bar = function() {};
delete foo // can be deleted;
delete bar // shouldn't be deleted;
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
I think you have some confusion about functions and object's prototype, I hope I will be able to clarify a bit.
First of all:
function foo_2 () {
console.log("I ma foo 2");
}
This is called "function declaration", where:
var foo_1 = function () {
console.log("I ma foo 1");
}
Is called "function expression". You can have detailed information here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression, with also the Function constructor example.
Since in javaScript every thing is an object, are two new objects created with name foo_1 and foo_2 in the previous code?
Yes, they're objects, and if you modify the prototype chains you will affect them:
Object.prototype.testName = "this is an experiment";
function test() {}
console.log(test.testName) // "this is an experiment"
But test.prototype is an object too, so if you do:
console.log(test.prototype.testName) // "this is an experiment"
The reason because you do not get the property correctly it's probably because is inherited, and the console you're using (node, browser) displays only the own properties of an object (like Object.keys), without crawl the whole prototype's chain.
That is the main reason because you shouldn't change the Object.prototype: you will affect mostly everything and you can't predict the consequences.
Finally, I was wondering if the these function definitions are equivalent to the following code:
this.foo = function() {
console.log("I ma foo");
}
It depends by what is this when you execute this code. They're mostly equivalent if you execute the code in the global scope, so this would be the global object (in browsers is window). However, there is a difference. Using this syntax, you would be able to delete the foo property, where you can't do that when is declared. So:
this.foo = function() {};
var bar = function() {};
delete foo // can be deleted;
delete bar // shouldn't be deleted;
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
I think you have some confusion about functions and object's prototype, I hope I will be able to clarify a bit.
First of all:
function foo_2 () {
console.log("I ma foo 2");
}
This is called "function declaration", where:
var foo_1 = function () {
console.log("I ma foo 1");
}
Is called "function expression". You can have detailed information here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression, with also the Function constructor example.
Since in javaScript every thing is an object, are two new objects created with name foo_1 and foo_2 in the previous code?
Yes, they're objects, and if you modify the prototype chains you will affect them:
Object.prototype.testName = "this is an experiment";
function test() {}
console.log(test.testName) // "this is an experiment"
But test.prototype is an object too, so if you do:
console.log(test.prototype.testName) // "this is an experiment"
The reason because you do not get the property correctly it's probably because is inherited, and the console you're using (node, browser) displays only the own properties of an object (like Object.keys), without crawl the whole prototype's chain.
That is the main reason because you shouldn't change the Object.prototype: you will affect mostly everything and you can't predict the consequences.
I said "mostly" because you can create objects that actually doesn't inherit from Object's prototype. You can do it using Object.create – or the non-standard where supported __proto__ :
var foo = {};
console.log(foo.testName) // "this is an experiment"
var bar = Object.create(null);
console.log(foo.testName) // undefined
Finally, I was wondering if the these function definitions are equivalent to the following code:
this.foo = function() {
console.log("I ma foo");
}
It depends by what is this when you execute this code. They're mostly equivalent if you execute the code in the global scope, so this would be the global object (in browsers is window). However, there is a difference. Using this syntax, you would be able to delete the foo property, where you can't do that when is declared. So:
this.foo = function() {};
var bar = function() {};
delete foo // can be deleted;
delete bar // shouldn't be deleted;
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
I think you have some confusion about functions and object's prototype, I hope I will be able to clarify a bit.
First of all:
function foo_2 () {
console.log("I ma foo 2");
}
This is called "function declaration", where:
var foo_1 = function () {
console.log("I ma foo 1");
}
Is called "function expression". You can have detailed information here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression, with also the Function constructor example.
Since in javaScript every thing is an object, are two new objects created with name foo_1 and foo_2 in the previous code?
Yes, they're objects, and if you modify the prototype chains you will affect them:
Object.prototype.testName = "this is an experiment";
function test() {}
console.log(test.testName) // "this is an experiment"
But test.prototype is an object too, so if you do:
console.log(test.prototype.testName) // "this is an experiment"
The reason because you do not get the property correctly it's probably because is inherited, and the console you're using (node, browser) displays only the own properties of an object (like Object.keys), without crawl the whole prototype's chain.
That is the main reason because you shouldn't change the Object.prototype: you will affect mostly everything and you can't predict the consequences.
Finally, I was wondering if the these function definitions are equivalent to the following code:
this.foo = function() {
console.log("I ma foo");
}
It depends by what is this when you execute this code. They're mostly equivalent if you execute the code in the global scope, so this would be the global object (in browsers is window). However, there is a difference. Using this syntax, you would be able to delete the foo property, where you can't do that when is declared. So:
this.foo = function() {};
var bar = function() {};
delete foo // can be deleted;
delete bar // shouldn't be deleted;
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete