returning object that returns functions

returning objects in c++
passing and returning objects in java
return object from function javascript
c++ passing objects by reference
objects as function arguments pass by value pass by reference in c++
object as function argument in c++ tutorialspoint
javascript function return object undefined
javascript return object literal

I need to write a function in JavaScript that takes a number and returns an object that returns chainable functions (without using OOP).



would outputs false.



would output: true

This is the code I have written:

const func = (obj) => { 
  const obj2 = {
    not: () => {
      return !obj
    equals: (num) => {
     return obj === num
  return obj2 

It works when I call func(3).not() or func(5).equals(5), but doesn't allow me to chain the functions so calling func(5).not().equals(5) returns an error saying that this is not a function.

What am I not seeing here?

You can use a closure to store both the initial input and the state of the operation:

const func = (input) => {
  let not = false
  const obj = {
    not: () => {
      not = !not
      return obj
    equals: (num) => {
      return not ? input !== num : input === num
  return obj;


JavaScript: function returning an object, It returns the value returned by the constructor if it's an object, or the object created at step 1 otherwise. var player = new GamePlayer("John Smith� A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function. The syntax for defining a function that returns an object by value is

That's a very weird way to compose functions. Let's think about what's actually happening.


// is equivalent to


// where

const not = x => !x;

const equals = x => y => x === y;

The simplest way to implement this chain would be as follows.

const equals = x => toBool(y => x === y);

const toBool = func => ({
    not: () => toBool(x => !func(x)),

const example1 = equals(4).not().not().func(3);
const example2 = equals(5).func(5);

console.log(example1); // false
console.log(example2); // true

How to pass and return object from a function in C++?, This webpage provides tutorial on passing object to function and returning object from function in C++ programming. Learn more And that’s it. A really nice shorthand for returning objects from an arrow function. Thankfully this ‘issue’ only applies to returning objects. For all other JavaScript types the implicit return works perfectly without this trick.

To return another object with the same methods that wraps the new value, simply call func again:

const func = (obj) => { 
  const obj2 = {
    not: () => {
      return func(!obj)
//    ^^^^^^^^^^^^^^^^^
    equals: (num) => {
      return obj === num
  return obj2 

Pass and return Object from a function in C++, In this tutorial we will see how to pass an object to a function as an argument and how to return an object from a function. Pass object to a function An. In C++ programming, object can be returned from a function in a similar way as structures. Example 2: Pass and Return Object from the Function In this program, the sum of complex numbers (object) is returned to the main() function and displayed.

You could take an object for return as interface and store value and negation.

var object = {
        func: function (value) {
            object.left = value;
            return object;
        not: function() {
            object.negation = !object.negation;
            return object;
        equals: function (value) {
            var result = value === object.value;
            return object.negation ? !result : result;
    func = object.func;


How to return object from function in JavaScript?, The return statement in function returns a value specified by the programmer. In JavaScript, a function can return nothing, by just using empty return statement. But without using even the statement, the function returns undefined. Finally, note that a function is free to define what its return value means. Some functions use return values as status codes, to indicate whether they succeeded or failed. Other functions return a calculated or selected value. Other functions return nothing. What the function returns and the meaning of that value is defined by the function’s

C++ Tutorial: Object Returning - 2020, When a function, either a member function or a standalone function, returns an object, we have choices. The function could return. A reference to an object; A� When using return by reference or address, make sure you are not returning a reference to, or the address of, a variable that will go out of scope when the function returns! Quiz time Write function prototypes for each of the following functions.

Function return values, There's one last essential concept about functions for us to discuss — return values. Some functions don't return a significant value, but others� Object& return_Object(); if the object returned has a greater scope than the function. For example, you can use it if you have a class where it is encapsulated. If you create an object in your function, use pointers.

How to return an object literal from a JavaScript constructor, So, if we want to have a constructor function that returns an object literal, we can literally (sorry : – ) just use the “return” statement, and put an object on the other� If you rather use functions then one easy method is to assign the function to a variable and if more than the return value is returned the var is an array and the return value will be the last item in the array. – DaSmokeDog Feb 21 '19 at 19:08 1 Well, what do you expect from a command called Write-Output?

  • Why would that work? not and equals return booleans, which don't have the equals or not properties. If they returned instances themselves, you'd have to provide a method to get the result out, too.
  • .not() needs to return an object. It would be easier if you coded this using a class.
  • "without using OOP" - that makes no sense. Having objects with methods is OOP.
  • "I need to write ...." I doubt you need to write your code like that. You want to write your code like that, but it's a bad idea.
  • Awesome! This is what I was trying to do. Thank you!
  • I find it always fascinating how much closures look like the private state of objects (and vice-versa, depends where you're looking from, of course).
  • That doesn't do what you expect it to do. For example, consider the expression func(3).not().not().equals(3) which should return true but instead returns false.
  • The expression func(3).not().not().equals(3) should evaluate to !!(3 === 3) but in your example it evaluates to 3 === !!3.
  • @AaditMShah !!3 is true, which is not equal to 3, so from my perspective that's expected. The OP didn't give this particular example, so we're not sure which one he meant. I do like your answer as well, though :-)
  • Yeah, I'm not sure what the OP is trying to do. XD
  • Thank you! I'll try this!