You would call it as an object property: obj.func1(); Answer from ArielLeslie on forum.freecodecamp.org
Top answer
1 of 2
78

Modern ES6 Approach

You no longer need to specify the function keyword when defining functions inside objects.

First option with named functions:

const myObj = {
  myMethod(params) {
    // ...do something here
  },
  myOtherMethod(params) {
    // ...do something here
  },
  nestedObj: {
    myNestedMethod(params) {
      // ...do something here
    }
  }
};

Second option with anonymous functions:

const myObj = {
  myMethod: (params) => {
    // ...do something here
  },
  myOtherMethod: (params) => {
    // ...do something here
  },
  nestedObj: {
    myNestedMethod: (params) => {
      // ...do something here
    }
  }
};

Pre ES6 style:

const myObj = {
  myMethod: function myMethod(params) {
    // ...do something here
  },
  myOtherMethod: function myOtherMethod(params) {
    // ...do something here
  },
  nestedObj: {
    myNestedMethod: function myNestedMethod(params) {
      // ...do something here
    }
  }
}; 

Note: In the first example the functions are named and have their own this-context. In the second example, the this-context from outside of the functions is used. In the third example, the functions are not named, but have their own this-context.

So while all methods seem similar, they are a bit different.

2 of 2
48

you need to define the objects like this :

var argument1 = {
    myvar : "12",
    mymethod : function(test) { return something; }
}

then call mymethod like:

argument1.mymethod(parameter);

or the deeper version :

var argument1 = {
    argument2 : {
       mymethod : function(test) { return something; }
    }
} 

then:

argument1.argument2.mymethod(parameter);
Discussions

javascript - Using function inside function inside an object - Stack Overflow
The code you currently have is not valid JavaScript. You're trying to handle functions as an object inside a function (??). More on stackoverflow.com
🌐 stackoverflow.com
javascript - Function inside object - Stack Overflow
Well, i don't know if the return in the transform function can work, but, someone know if there is a way where i can get something similar to this? Put a function inside an object... var t = $(this). More on stackoverflow.com
🌐 stackoverflow.com
javascript - Functions in object literals - Code Review Stack Exchange
I have 2 questions about functions inside object literals. I have included the workarounds I've been using, but they seem hackish. Is there a better way to do this? ns = { a: function (x, y) { More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
February 21, 2014
Is it possible to let a function access an entire object's properties by passing it a single property?
The short answer is no, you need to send it the entire object. You should probably send the object and the relevant key. Something like this: const myObject = { a: 1, b: 2, c: 3 }; const myFunction = (obj, param) => { if (obj[param] && obj['c']) { return obj[param] + obj['c']; } return obj.c; } console.log(myFunction(myObject, 'a')); console.log(myFunction(myObject, 'b')); More on reddit.com
🌐 r/learnjavascript
13
6
June 8, 2024
🌐
Reddit
reddit.com › r/javascript › calling a function inside an object?
r/javascript on Reddit: calling a function inside an object?
April 23, 2017 -

I have an object named "subject" and a function named "getDueDate" I would like to call that function inside of a subject object that would return value when called upon an atribute.

  var subject = {
				name: 'Person',
				birth_day: new Date('2017-04-21'),
				due_date: getDueDate(subject.birth_day, subject.current_cycle),
				current_cycle: 1,
				last_water_date: null,
				next_water_date: null,
		};
	
		var getDueDate = function(birth_day, cycles) {
				var result = new Date(birth_day);
				console.info('result_ ' + result);
				result.setDate(result.getDate() + cycles);
				console.info('result_1_ ' + result);
				return result;
		};

when I try to parse subject.due_date I get an error getDueDate undefined, what am I doing wrong or it's not even possible to do that?

🌐
Tutorjoes
tutorjoes.in › JS_tutorial › function_inside_object_in_javascript
Function Inside Object in JavaScript
Overall, methods are an essential part of object-oriented programming in JavaScript and are a powerful tool for working with complex data structures. function checkEligiblity(){ if(this.age>=18){ console.log(`${this.firstname} age is ${this.age} eligible for vote`); }else{ console.log(`${this.firstname} age is ${this.age} not eligible for vote`); } } const user1={ firstname:"Joes", age:35, eligiblity:checkEligiblity } user1.eligiblity(); const user2={ firstname:"Sara", age:12, eligiblity:checkEligiblity } user2.eligiblity();
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-call-function-inside-object
How to call a Function in an Object in JavaScript | bobbyhadz
You can call a function inside an object by declaring the function as a property on the object and invoking it, e.g. `obj.sum(2, 2)`.
🌐
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.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Object methods, "this"
When a function is called in the “method” syntax: object.method(), the value of this during the call is object. Please note that arrow functions are special: they have no this. When this is accessed inside an arrow function, it is taken from outside.
Find elsewhere
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript - MDN Web Docs - Mozilla
February 21, 2026 - 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.
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Functions
Functions - JavaScript - MDN Web Docs
In the formatNumber(2) call, the number 2 is the function's argument: it's the value that is actually passed to the function in the function call. The argument value can be accessed inside the function body through the corresponding parameter name or the arguments object...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Function object, NFE
April 6, 2025 - Usually taken from the function definition, but if there’s none, JavaScript tries to guess it from the context (e.g. an assignment). length – the number of arguments in the function definition. Rest parameters are not counted. If the function is declared as a Function Expression (not in the main code flow), and it carries the name, then it is called a Named Function Expression. The name can be used inside to reference itself, for recursive calls or such.
🌐
Kevin Chisholm
blog.kevinchisholm.com › javascript › include-function-as-javascript-object-member
How To Include A Function as a JavaScript Object Literal Member | Kevin Chisholm - Blog
February 10, 2021 - You define a function as an object member simply by assigning an anonymous function to the key, instead of the usual scalar value. ... The output for Example # 1 would be: “Hi, my name is: Don Draper, my address is: 123 E. Maple Ave., and ...
🌐
IQCode
iqcode.com › code › javascript › function-inside-object-javascript
function inside object javascript Code Example
var person = { name: "Fred", sayName: function() { console.log(this.name); } }; person.sayName();
🌐
EyeHunts
tutorial.eyehunts.com › home › function inside object javascript
Function inside object JavaScript - Tutorial - By EyeHunts
March 23, 2023 - By defining a function as a method of an object, you can access the properties of that object using the this keyword inside the method. const myObject = { myProperty: 'hello', myMethod: function() { console.log(this.myProperty); } }; ...
🌐
Codecademy
codecademy.com › forum_questions › 50525cf39cdbb9000200611e
Why are "functions" inside objects called "methods"? | Codecademy
The term method bears more information, because you know immediately that there’s some object that this method belongs to (and if the object is obj, you have to call that method using dot notation obj.methodName()), while you can call other functions without prepending an object and a dot to them. ... In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language. Beginner Friendly.Beginner Friendly4 Lessons4 Lessons ... Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
Top answer
1 of 4
4

Problem 1

Just assign them one at a time, once the object already exists:

ns = {};
ns.a = function (x, y) { return x+y; };
ns.b = ns.a;

To answer specifically whether there's any other workaround if you're dead set on declaring these inside the object literal syntax: no.

Problem 2

Looks good enough to me, though I'd try to avoid using the object's name so deep into its definition.

Consider using prototyping instead.

2 of 4
3

Problem 1 When creating an object literal, this scoped by whatever function is constructing the object. This allows you to do:

this.foo = 'bar';
var obj = { foo: this.foo };
console.log(obj.foo); // 'bar'

In fact the object doesn't exist until the end of the object literal, so there is no object to reference.

The correct work around for you issue would be like so. Because you cannot use the same value for multiple keys in an object literal.

ns = { a: function() {} };
ns.c = ns.a; // the same function now is on .a and .c

Problem 2

This has nothing to do with scope, but instead context (the value of this). In function invocations, the dot syntax is what provides the this. For example:

obj.someFn() // inside someFn: this === obj

It's the syntax that provides this. So when there is no dot accessor, this defaults to the global object (window in a browser).

The usual work around is to simply save a reference to the current object which is shared with any functions in the same scope.

d: function(x, y) {
  var self = this;
  var add = function(x, y) {
    self.a(x, y);
  };
  add(3, 4);
}

Or use call or apply to explicitly set the function context, which overrides the default binding of this with whatever object you want.

d: function(x, y) {
  var add = function(x, y) {
    this.a(x, y);
  };
  add.call(this, 3, 4);
}
🌐
TutorialsPoint
tutorialspoint.com › How-can-we-use-a-JavaScript-function-as-an-object
How can we use a JavaScript function as an object?
July 14, 2022 - <html> <body> <h2> Use Javascript function as an object. </h2> <p> Create method inside the function, initialize value of the function variable, and use it as an object.
🌐
JavaScript in Plain English
javascript.plainenglish.io › using-functions-in-objects-in-es6-javascript-4894af82d1d8
How to Use Functions in Objects in ES6 JavaScript | by Codecupdev | JavaScript in Plain English
April 29, 2022 - How to Use Functions in Objects in ES6 JavaScript A brief introduction on how to use functions in objects in ES6 We can add functions to our objects and when we do this we call them methods. Let’s …