-2

This is my demo code:

ngOnInit{
 let p1 : Person = {};
 console.log(p1); //Object { }
 this.setNull<Person>(p1);
 console.log(p1); //Object { }
}

private setNull<T>(obj : T){
 obj = null;
}

My goal is to set p1 to null after calling setNull().

I hope to have explined well my problem. Thanks in advance.

Elkin
  • 880
  • 2
  • 12
  • 26
  • 2
    Why don't you directly set p1 = null; ? – Prawin soni Oct 01 '19 at 14:41
  • why do you want to pass it a function? you directly set `null` to `p1`. – Pranay Kumar Oct 01 '19 at 14:42
  • An assignment to the parameter name in `setNull` is **not** going to change what `ngOnInit` sees. This isn't related to Angular or TS, just plain JS. – jonrsharpe Oct 01 '19 at 14:42
  • @Prawinsoni This is demo code. The actual code is larger. My goal is just to set an object passed by reference to null, if possible – Elkin Oct 01 '19 at 14:42
  • What makes you think you are passing an object by reference? – Scott Hunter Oct 01 '19 at 14:45
  • It's not possible because the "obj" in your setNull function is another variable that is just holding the reference, so if you assign null now the "obj" is set to null. – Prawin soni Oct 01 '19 at 14:53
  • Related (not *quite* dupe or am I being conservative?): https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – T.J. Crowder Oct 01 '19 at 15:06

3 Answers3

2

My goal is to set p1 to null after calling setNull().

You can't, because it isn't "passed by reference." It's an object reference passed by value. "Pass by reference" is a term of art meaning a reference to the variable is passed into the function. Neither TypeScript nor JavaScript has pass-by-reference, at all.

In this specific case, it makes sense just to assign null to p1. But if you had a more general case, perhaps if setNull doesn't always set it null, or does something else (the classic case is "closing" p1 in some way), you could use the return value of setNull to set p1, e.g.:

p1 = someFunction(p1);

...where someFunction returns a value to use to update p1 (perhaps null).

Alternately, make p1 a property of a mutable object, pass that object in, and have the function set the p1 property to null where appropriate.

But if you're unconditionally assigning null without the function having any other reason for being, just...do that, assign null to p1 without a function.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Instead of calling a function that is returning null, why can't we just assign null? – Prawin soni Oct 01 '19 at 14:54
  • 1
    @Prawinsoni - I said that above. *"In this specific case, it makes sense just to assign `null` to `p1`."* The only reasons for doing it with a function are if the function does something useful (classic case is "closing" something `p1` represents) or if the function may or may not return `null` based on some condition(s). I've beefed that up a bit. – T.J. Crowder Oct 01 '19 at 14:56
1

Its displaying correctly only. And yes in JS, Object are passed by ref. p1 is holding ref which is pointed {} in memory.

Now you passed that reference to method and now obj is pointing to {}. At this stage both are pointers. You just made obj = null which means obj now is pointing to null. However, p1 is still pointing to {}.

To prove it is pass by ref,

do below demo :

var p1 = { "name" : "demo" }
function passByRef (obj) {
   obj.name = "demo updated";
}
//call fun with p1
passByRef(p1);
// print p1.name -> it will be updated one
console.log(p.name);

Thus, JS is like Java. Functions argument are called by Reference.

vipul patel
  • 668
  • 4
  • 7
-2

You can declare an Interface and make that element p1 be null or not like this:

export Interface IPerson {
bla bla bla
p1? : number;(array, string or whatever)

}

that way (ending the prop. with ?) property is accepted as null as full

Qiqke
  • 486
  • 5
  • 19