2

So I can define a property on a function from the outside such as: createNewPerson.hmm = 3; and I get the result 3 when I use the alert. However if I try to do the same but from within a function (as in define a property within a function) it doesn't work.. I tried all the commented lines below. Is there a different way to achieve this or is it simply not possible? If it's not possible, then how does javascript append the .hmm property to the function and later call it?

Full code:

function createNewPerson(name) {
//var hello = "hgfgh";
//hello: "hgfgh"
//hello = hgfgh;
//this.hello = hgfgh;

}
createNewPerson.hmm = 3;
alert(createNewPerson.hmm);
alert(createNewPerson.hello);
user8783104
  • 175
  • 2
  • 11
  • 1
    You can use `createNewPerson.hmm = "something";` (even from within the function). But if the function is anonymous then it's a different story. – ibrahim mahrir Nov 13 '17 at 20:02
  • are you sure you tried `this.hello = 'hgfgh'`? what you posted doesn't have quotations but otherwise is correct – Robbie Milejczak Nov 13 '17 at 20:03
  • 1
    `this.hello` is the right syntax, but you need to create an instance. `new createNewPerson()` – Sterling Archer Nov 13 '17 at 20:03
  • 1
    @RobbieMilejczak: No; that's not what he's doing. – SLaks Nov 13 '17 at 20:03
  • @SLaks is he not building an object constructor? that is what it looks like... – Robbie Milejczak Nov 13 '17 at 20:04
  • 1
    There used to be an `arguments.callee` option if you don't have a named function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee has more information on why that was obsoleted. – Jacob Nov 13 '17 at 20:04
  • 1
    @Robbie: without using `new` to create an object the `this` inside the function actually points to the global object, so `createNewPerson.hello` would still be undefined. – david25272 Nov 13 '17 at 22:23
  • 1
    https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work – david25272 Nov 13 '17 at 22:29
  • @ibrahimmahrir thanks! so it's somewhat like a static access? Also why does the syntax {property: "value"} not work within a function, since a function is essentially an object.. – user8783104 Nov 14 '17 at 16:36
  • @user8783104 Read the [article about scope](http://web.archive.org/web/20110725013125/http://www.digital-web.com/articles/scope_in_javascript/) that david linked. – Guillaume CR Nov 15 '17 at 16:08
  • @user8783104 I think I understand your question a little better. The key part to my epiphany was that even though functions are objects, the execution context (or rather, the activation/variable object) inside a function is not the function itself. They are 2 separate objects. 'this' is assigned at the creation stage of the activation object, and does not necessarily contain the properties that you assigned to the function object. This is what constructors are for. – Guillaume CR Nov 15 '17 at 19:09
  • I understand the idea of constructors to create objects but not how functions are objects themesleves that can have properties defined that way.. Well I actually understand that functions are objects themselves, but always thought of functions as objects with the name of the of the function as the key and the statements as the value, but I'm confused here how a property can be defined on the function or within it referencing the same function? – user8783104 Nov 16 '17 at 16:01

2 Answers2

1

I think you are trying to create objects. In javascript, you do it this way:

function Test(){
  this.foo = "bar";
  this.func = function(){
    console.log(this.foo);
  }
}

const test = new Test();
test.foo = "baz";
test.func(); //Will print "baz".

note the use of "new". This is what enables the mechanism of allowing code to modify properties of object. The modified values of the properties is then accessible by the object itself.

Guillaume CR
  • 2,878
  • 1
  • 15
  • 30
0

Hi please try below code,

jsFiddle

function createNewPerson(name) {
//var hello = "hgfgh";
//hello: "hgfgh"
//hello = hgfgh;
this.hello = "hgfg";
this.name = name;
}
var cnp = new createNewPerson('de');
cnp.hmm = 3;
alert(cnp.hmm);
alert(cnp.hello);
alert(cnp.name);
Ferhat BAŞ
  • 757
  • 6
  • 11