Yes,
katanais an object (created using the{ ... }notation). "use" is the name of the property of the object whose value will be the anonymous function (which is also an object).The function inverts the value of
isSharp(so fromtruetofalseorfalsetotrue).It is asserting that
isSharpis something which does not evaluate to true (this is nearly everything exceptundefined,null,false,0, etc). In this case, sinceisSharpis always eithertrueorfalse, it is asserting that it isfalse.
The main point (and cool part) of the sample is this line:
katana.use();
This first fetches the value of the "use" property from the katana object (that's the katana.use part). The value is the anonymous function from before. Then, that function is executed (that's the () part). The really cool part is that it is executed on behalf of the katana object -- that means this in the anonymous function is a reference to the katana object when it's called that way.
JavaScript - function as an object property - Stack Overflow
Functions inside objects
Difference between an object and a function?
what is the difference between objects and functions?
Videos
Yes,
katanais an object (created using the{ ... }notation). "use" is the name of the property of the object whose value will be the anonymous function (which is also an object).The function inverts the value of
isSharp(so fromtruetofalseorfalsetotrue).It is asserting that
isSharpis something which does not evaluate to true (this is nearly everything exceptundefined,null,false,0, etc). In this case, sinceisSharpis always eithertrueorfalse, it is asserting that it isfalse.
The main point (and cool part) of the sample is this line:
katana.use();
This first fetches the value of the "use" property from the katana object (that's the katana.use part). The value is the anonymous function from before. Then, that function is executed (that's the () part). The really cool part is that it is executed on behalf of the katana object -- that means this in the anonymous function is a reference to the katana object when it's called that way.
1) Katana is an object. Katana.use is a function. Its a property that contains a function as value. The value it contains happens to be an anonymous function.
The distinction is that Katana.use is a property of Katana and that the value of Katana.use is a function. use is a key defined on Katana since Katana["use"] also works.
2) It's setting isSharp to NOT isSharp so either true -> false or false -> true
3) the assert is saying katana.isSharp === false which it should be since it was orginally true but then set to false.
Is a function just a type of object? Where my understanding is at now, is that functions you can call and assign parameters to. But cant you do the same with objects?
Lol help me please :(
So, I started learning coding by learning flow charts and how the computer works and then I have learned the basis of html and css. Now I'm shifting to Javascript. If I have understood the difference between variable (box) and array ( weekly pillbox with 0,1,2 ecc instead of Monday ecc) I didn't understand the difference between object and function. They look too abstract to me. If you could compare them to sth that can be touched , what would it be ? thanks in advance
If a variable is a box and an array is a weekly pillbox what are objects and functions? thanks again
You can't (as far as I know) do what you're asking, but hopefully this will clear up any confusion.
First, most objects in Javascript inherit from the Object prototype.
// these do the same thing
var foo = new Object();
var bar = {};
console.log(foo instanceof Object); // true
console.log(bar instanceof Object); // true
Second, functions ARE objects in Javascript. Specifically, they are objects that inherit from the Function prototype, which itself inherits from the Object prototype. Check out Function on MDN for more information.
// various ways of creating functions
var foo = new Function();
var bar = function(){};
function baz(){};
// all functions are Function objects, and therefore Object objects
console.log(foo instanceof Function); // true
console.log(foo instanceof Object); // true
If you create a value that is an Object object first, you can't (as far as I know) convert it to a Function object later. What you CAN do however is create a new function, copy the properties from your old object to this function, and then reassign that function to the same variable, discarding the old object.
You can quickly copy the properties of one object to another with Object.assign(target, source).
var foo = { baz: "qqqq" };
var bar = function () { return 1; }; // create a new function
Object.assign(bar, foo); // copy the properties of foo to it
foo = bar; // assign the function to foo
console.log(foo instanceof Function); // true
console.log(foo()); // 1
console.log(foo.baz); // "qqqq"
But this isn't something you would typically do in practice. It's generally not recommended to add properties to a standard object like functions in the first place.
Finally, anticipating a possible question, you can't change the body of a function after it has been created. The function body is stored internally and not exposed as a property.
There doesn't appear to be a standard way to do it, but this works.
WHY however, is the question.
function functionize( obj , func )
{
out = func;
for( i in obj ){ out[i] = obj[i]; } ;
return out;
}
x = { a: 1, b: 2 };
x = functionize( x , function(){ return "hello world"; } );
x() ==> "hello world"
There is simply no other way to acheive this, doing
x={}
x()
WILL return a "type error". because "x" is an "object" and you can't change it. its about as sensible as trying to do
x = 1
x[50] = 5
print x[50]
it won't work. 1 is an integer. integers don't have array methods. you can't make it.
I'm not sure this will answer your question but may give you some insight. Consider the following example:
var Person = (function () {
var Person = function (name) {
this.name = name;
}
Person.greet = function () {
console.log("Hello!");
}
Person.prototype = {
greet: function () {
console.log('Hello, my name is ' + this.name);
}
};
return Person;
})();
var bob = new Person("Bob");
Person.greet(); // logs "Hello!"
bob.greet(); // logs "Hello, my name is Bob
The function object "Person" has a direct 'greet' property that is a Function. OOP-wise, you can almost think of that as a static method that can be called directly from the Person Function (Person.greet()). Once you "instantiate" a person object from the Person constructor, that new object "bob" now references it's methods from the Person.prototype object. Now when you call bob.greet(), it uses the greet function in the prototype object.
Hope that helps.
As you say so yourself: you have a function object. Functions are objects in JS, just like Object literals, arrays, or anything else: a function can be assigned properties and methods at will:
var someAnonFunction = function(foo)
{
console.log(this);
console.log(this === someAnonFunction);//will be false most of the time
};
someAnonFunction.x = 123;//assign property
someAnonFunction.y = 312;
someAnonFunction.divide = function()
{
console.log(this === someAnonFunction);//will be true most of the time
return this.x/this.y;//divide properties x & y
};
someAnonFunction.divide();
In this case, the function object, referenced by someAnonFunction has been assigned a reference to the anonymous function, called divide (well, the reference to an anonymous function was dubbed divide anyway). So there is no prototype involvement at all here. Mind you, as you say so yourself: all objects can be traced back to Object.prototype, just try this:
console.log(someAnonFunction.toString === Function.prototype.toString);//functions are stringified differently than object literals
console.log(someAnonFunction.hasOwnProperty === Object.prototype.hasOwnProperty);//true
Or, perhaps this is more clear: a simple scheme of how a method/property call is resolved to a value in JS:
[ F.divide ]<=========================================================\ \
F[divide] ===> JS checks instance for property divide | |
/\ || | |
|| || --> property found @instance, return value-------------------------------| |
|| || | |
|| ===========> Function.prototype.divide could not be found, check prototype | |
|| || | |
|| ||--> property found @Function.prototype, return-----------------------| |
|| || | |
|| ==========> Object.prototype.divide: not found check prototype? | |
|| || | |
|| ||--> property found @Object.prototype, return---------------------|_|
|| || |=|
|| =======>prototype is null, return "undefined.divide"~~~~~~~~~~~~~~~|X|
|| \ /
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~< TypeError can't read property 'x' of undefined
It therefore follows, that if you want the code above to work using prototypes, you'll have to augment a prototype of sorts (in this case, the Function.prototype). Do know that this isn't to be recommended, in fact changing "native" prototypes is often frowned upon. Still:
Function.prototype.divide = function (a, b)
{
a = +(a || 0);//coerce to number, use default value
b = +(b || 1) || 1;//division by zeroe is not allowed, default to 1
return a/b;
};
function someFunction ()
{
return 'someString';
};
var another = function(a, b)
{
return a + b;
};
someFunction.divide(12, 6);//will return 2
another.divide(12, 4);//3
In both cases, the function object, referenced by the name (someFunction or another) will be scanned for a property called divide, which isn't found. Then however it'll scan the Function.prototype, where such a property is found.
If that weren't the case, JS would also check the Object.prototype, if that failed, it will eventually throw an error.
I've posted quite lengthy answers on SO on this subject a while back:
What makes my.class.js so fast? (deals with prototype chains)
Objects and functions in javascript (recap of functions <=> objects <=> constructors)
What are the differences between these three patterns of "class" definitions in JavaScript? (some more info, still)
Javascript - Dynamically change the contents of a function (vaguely touches on the anonymous functions, assigned to variables and properties and changing their context)