Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set null context in call function?

function test(){         
     if(this === null){
        console.log("This is null");
     }else{
         console.log("This is Object");
     }
 }
test.call(null);
test.call({});

Output :

This is Object.

This is Object.

But I expect Output :

This is Null.

This is Object.

Why Null not set in context ?

like image 665
Obeth Samuel Avatar asked Jan 15 '18 05:01

Obeth Samuel


People also ask

How to check for null in a function?

ISBLANK is the function for NULL. use ISBLANK to check NULL. Your expression will be like below, Please check in editor for accuracy. 04-08-2020 09:28 PM It's not quite correct though because if they use the blank in a calculation it will count as 0 not NULL.

Is it possible to invoke a method on the constant null?

This program looks as though it is ought to throw a NullPointerException. However, the main method invokes the greet method (fun) on the constant null, and you can’t invoke a method on null. But, when you run the program, it prints “Welcome to GeeksforGeeks!!”.

How to check for null in Dax?

ISBLANK is the function for NULL. use ISBLANK to check NULL. Your expression will be like below, Please check in editor for accuracy. 04-08-2020 09:28 PM It's not quite correct though because if they use the blank in a calculation it will count as 0 not NULL. Which will introduce errors. 07-03-2018 05:51 AM Blank () is working in Dax.

How do you reference a context variable in a formula?

You reference a context variable in a formula by using the variable's column name. For example, UpdateContext ( { ShowLogo: true } ) creates a context variable named ShowLogo and sets its value to true. You can then use the value of this context variable by using the name ShowLogo in a formula.


2 Answers

Quoting from MDN

if the method is a function in non-strict mode, null and undefined will be replaced with the global object and primitive values will be converted to objects.

This explains why you get an object when you call test.call(null);. When null is passed here, this inside test() will be global object Window.

For the desired behavior, use strict mode.

function test() {
  "use strict";
  if (this === null) {
    console.log("This is null");
  } else {
    console.log("This is Object");
  }
}
test.call(null);
test.call({});

Quoting from ES6 Specifications for strict mode

If this is evaluated within strict mode code, then the this value is not coerced to an object. A this value of null or undefined is not converted to the global object and primitive values are not converted to wrapper objects. The this value passed via a function call (including calls made using Function.prototype.apply and Function.prototype.call) do not coerce the passed this value to an object

like image 176
Tushar Avatar answered Oct 20 '22 21:10

Tushar


What does the 'this' indicate? you can use console.log(this); to know it. But as answer, use an input (here I called it input) and test it.

   function test(input){
 if(input=== null){
    console.log("This is null");
 }else{
     console.log("This is Object");
 }
 }
test(null);
test({});
like image 4
Elnaz Avatar answered Oct 20 '22 20:10

Elnaz