1. Yes, katana is 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).

  2. The function inverts the value of isSharp (so from true to false or false to true).

  3. It is asserting that isSharp is something which does not evaluate to true (this is nearly everything except undefined, null, false, 0, etc). In this case, since isSharp is always either true or false, it is asserting that it is false.

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.

Answer from Cameron on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Function
Function - JavaScript | MDN
The Function object provides methods for functions. In JavaScript, every function is actually a Function object.
🌐
DoFactory
dofactory.com › javascript › function-objects
JavaScript Function Objects
We see that a function is indeed an object. JavaScript functions are a special type of objects, called function objects. A function object includes a string which holds the actual code -- the function body -- of the function. The code is literally just a string.
Discussions

JavaScript - function as an object property - Stack Overflow
Hey everyone, this is #23 from John Resig Advanced JavaScript http://ejohn.org/apps/learn/#23, called What happens if a function is an object property. 1) regarding vocabulary, the variable ... More on stackoverflow.com
🌐 stackoverflow.com
Functions inside objects
How can i call a function if that whole function is stored in an object? A challenge suggested that objects can store functions, i tried calling a function which was inside an object, but didn’t work. So what is the use of it being stored in an object? Exemple: Const obj = { func1: function ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
October 29, 2021
Difference between an object and a function?
Object is at the root of the type hierarchy. Everything that's not a primitive(*) is an object. An array is an object. A function is an object. A regex is an object. And so on. You can only use the function-call operator with a function. Generally speaking, when talking about the type of something you use the most-specific (or most-derived) type. So if someone says something like "this function takes an object", that means that it takes a plain object, it doesn't take a function or an array or anything else, despite those also being objects. (*) Despite not being objects, primitives will auto-box themselves using object wrapper classes, so they act like objects under most circumstances. More on reddit.com
🌐 r/javascript
17
15
March 1, 2016
what is the difference between objects and functions?
A function (sometimes also called a method or subroutine) is essentially a collection of reusable commands. An object, in JavaScript, is basically anything that isn't a primitive (i.e., not a string, number, boolean, Symbol). An array is a type of object. A function is a type of object. It can basically be anything more complex than a primitive. You can create them pretty arbitrarily. Functions being objects is a bit special to JavaScript. In most other languages, they are something else entirely. More on reddit.com
🌐 r/learnjavascript
20
10
January 31, 2022
🌐
W3Schools
w3schools.com › js › js_object_methods.asp
JavaScript Object Methods
Methods are actions that can be performed on objects. Methods are functions stored as property values.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Functions
Functions - JavaScript | MDN
In JavaScript, functions are first-class objects, because they can be passed to other functions, returned from functions, and assigned to variables and properties. They can also have properties and methods just like any other object.
Top answer
1 of 3
30
  1. Yes, katana is 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).

  2. The function inverts the value of isSharp (so from true to false or false to true).

  3. It is asserting that isSharp is something which does not evaluate to true (this is nearly everything except undefined, null, false, 0, etc). In this case, since isSharp is always either true or false, it is asserting that it is false.

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.

2 of 3
1

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.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object
Object - JavaScript | MDN
Points to the object which was used as prototype when the object was instantiated. ... The constructor function that created the instance object. For plain Object instances, the initial value is the Object constructor.
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › what is the difference between objects and functions?
r/learnjavascript on Reddit: what is the difference between objects and functions?
January 31, 2022 -

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

Top answer
1 of 13
13
Given that a variable is a box that holds values, an object is a value that can consist of multiple other boxes. let myVariable = 5; // a single box holding the value 5 let myVariableCollection = new Object(); // container for multiple other boxes myVariableCollection.first = 'Hello'; myVariableCollection.second = 'there'; In fact an array is also a kind of object. It's a specialized object that has an ordered collection of its own boxes for values, starting at 0 (Monday) and moving up from there. let myOrderedCollection = new Array(); // container for boxes, but ordered myVariableCollection[0] = 'Hello'; myVariableCollection[1] = 'there'; Both of the examples above are using a long form way of defining objects and arrays, but they can be simplified using a literal syntax. Object literals use curly braces ({}) whereas arrays use square ([]). Objects need the names (aka "keys" or "property names") for the other values to be included, but array value numbers (or "indexes"/"indices") are figured out for you automatically. // shorthand, literal versions let myVariableCollection = { first: 'Hello', second: 'there' }; let myOrderedCollection = ['Hello', 'there']; // automatically uses 0 and 1 Functions are collections of code. They define a set of instructions (lines of code) to be run when they're called. This lets you perform actions multiple times without having to write the same code over and over again (among other things). For example you could have something written in code like this let num = 0; num = num + 1; num = num * 10; alert(num); // alerts 10 num = num + 1; num = num * 10; alert(num); // alerts 110 num = num + 1; num = num * 10; alert(num); // alerts 1110 But there are a lot of repeated steps here. If you put the repeated part in a function, you'd only have to call that function once for each set of repeated steps. let num = 0; function calculate () { num = num + 1; num = num * 10; alert(num); } calculate(); // alerts 10 calculate(); // alerts 110 calculate(); // alerts 1110 There's a lot more you can do with functions, but that's the general idea; they run code for you. What's interesting about functions is that they are themselves values. The function keyword above is defining a variable with the name calculate which is a box containing the function value. Using the parenthesis (()) it what allows you to call the function to run its code, but otherwise it also acts like an object containing its own values. alert(calculate.name) // alerts "calculate" Just like arrays, functions are specialized objects that do what normal objects do (contain other boxes) but also has the special ability to run code when called.
2 of 13
12
A function (sometimes also called a method or subroutine) is essentially a collection of reusable commands. An object, in JavaScript, is basically anything that isn't a primitive (i.e., not a string, number, boolean, Symbol). An array is a type of object. A function is a type of object. It can basically be anything more complex than a primitive. You can create them pretty arbitrarily. Functions being objects is a bit special to JavaScript. In most other languages, they are something else entirely.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript | MDN
1 month ago - To define an object type, create a function for the object type that specifies its name, properties, and methods. For example, suppose you want to create an object type for cars. You want this type of object to be called Car, and you want it to have properties for make, model, and year.
🌐
Codecademy
codecademy.com › forum_questions › 4fc0fd8dda2f27000301d865
What's really the difference between an object and a function? | Codecademy
A function is an object, actually. In JavaScript, there are primitives (boolean, number, string, null, undefined), and then there are objects. So if something is not a primitive, it’s an object.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Function object, NFE
April 6, 2025 - Functions are objects. ... name – the function name. Usually taken from the function definition, but if there’s none, JavaScript tries to guess it from the context (e.g.
Top answer
1 of 9
30

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.

2 of 9
26

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.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-object-methods
JavaScript Object Methods - GeeksforGeeks
July 30, 2025 - Object Methods in JavaScript can be accessed by using functions. Functions in JavaScript are stored as property values.
Top answer
1 of 5
58

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.

2 of 5
26

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)

🌐
W3Schools
w3schools.com › js › js_objects.asp
JavaScript Objects
To create an object type we use an object constructor function. ... When used in an object method, this refers to the object. ... Getters and setters allow you to define Object Accessors (Computed Properties).
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript object methods
JavaScript Object Methods
September 1, 2008 - You can either directly add a method to the object or add it as a property value. The method can also take the parameters and return the value. Object methods are a powerful way to add functionality to objects.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Object methods, "this"
If there’s this inside a function, it expects to be called in an object context. ... If you come from another programming language, then you are probably used to the idea of a “bound this”, where methods defined in an object always have this referencing that object. In JavaScript this is “free”, its value is evaluated at call-time and does not depend on where the method was declared, but rather on what object is “before the dot”.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects
Standard built-in objects - JavaScript | MDN
The host objects available in browser contexts are documented in the API reference. For more information about the distinction between the DOM and core JavaScript, see JavaScript technologies overview. These global properties return a simple value. They have no properties or methods. ... These global functions—functions which are called globally, rather than on an object—directly return their results to the caller.
🌐
DEV Community
dev.to › hy_piyush › 10-important-javascript-object-methods-everyone-must-know-in-2023-398k
10 Important JavaScript Object Methods everyone must know in 2023 - DEV Community
January 23, 2023 - JavaScript object methods are actions that can be performed on JavaScript objects. They are functions that are associated with an object and can be invoked using the dot notation or bracket notation on that object.