-1

I have been doing something like this for a while and I have never seen any errors. But, unfortunately, I have never been able to explain why this works.

The first line creates a variable which points to a function. The second line just adds func2 to func1 separated by a dot and defines a function.

If I add a var in front of func1.func2 then I see a compilation error.

func1.func2 = function(){};

Error

SyntaxError: missing ; before statement
var func1.func2 = function(){};

What type was func1 on first line 1 and what did it become on line 2.

var func1 = function(){};
func1.func2 = function(){};
gen_Eric
228k42 gold badges303 silver badges343 bronze badges
asked Nov 11, 2013 at 22:17
3
  • 1
    func1 is a Function Object and as such, can have properties dynamically assigned Commented Nov 11, 2013 at 22:30
  • 1
    When you do var func1, you are creating a new variable called func1. func1.func2 doesn't create a new variable, it adds a property to a variable that already exists. Commented Nov 11, 2013 at 22:31
  • 2
    If you add a var in front of it, then you're creating a variable and variable names can't have periods. That's why you get an error. Commented Nov 11, 2013 at 22:33

4 Answers 4

1

A function in JavaScript is an object too, that can have properties:

function func1(){} // a function object
console.log(func1.name); //=> "func1"

An anonymous function doesn't have a name but it's still a function object so:

What type was func1 on first line 1 and what did it become on line 2.

Still a function, with a property func2 containing another anonymous function.

answered Nov 11, 2013 at 22:31
Sign up to request clarification or add additional context in comments.

Comments

1

func1 is a function on both lines. You can confirm this using typeof:

var func1 = function(){};
typeof func1; // "function"
func1.func2 = function(){};
typeof func1; // "function"
answered Nov 11, 2013 at 22:30

Comments

1

One form of variable statement is:

var identifier [optional assignment];

The identifier in a variable statement must meet the rules for identifier names, one of which is that they can't contain a "." character. So:

var func1 [...]; 

is OK but:

var func1.func2 [...]; 

is not. The interpreter stops at the identifier because it's a syntax error and that's it, it can't proceed with the assignment.

answered Nov 11, 2013 at 22:44

Comments

1
func1.func2 = function(){};

If func1 is not defined you are trying to access a property of undefined which causes an exception to be thrown.

var func1.func2 = function(){};
// SyntaxError: Unexpected token .

JavaScript syntax does not allow a dot to be inserted in a variable's name and therefore the interpreter returns a syntax error error.

var func1 = function(){};
func1.func2 = function(){};

A function in javaScript is a Function object, therefore, as all javascript objecta a method can be added dynamically to it at run-time. So func1 is Function Object and func2 is another function object. This is the correct way to achieve what you are trying to do.

answered Nov 11, 2013 at 22:44

1 Comment

Actually, if func1 is not defined then you'd get a reference error. The "trying to access X of undefined" error usually happens when you are trying to access a non-existing property of an object. Because in that case, the return value is actually undefined.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.