Understanding the different ways this behaves is essential for writing effective and flexible JavaScript code. Global Context When used outside of any function or object, this refers to the global object. In browsers, this is usually the window object. console.log(this === window); // true ...
functionAverageJoe(){console.log(this)// {} 'new object'this.name="Joe"console.log(this)// {name: "Joe"}}newAverageJoe() Via the apply or call method Functions in JavaScript have access to two inbuilt methods:applyandcall. func.apply(thisArg,argArray)func.call(thisArg,arg1,arg2,......
const negativeInfinity = -Infinity; console.log(negativeInfinity); // Output: -Infinity console.log(typeof negativeInfinity); // Output: "number" console.log(negativeInfinity - 1); // Output: -Infinity (any number subtracted from -Infinity is still -Infinity) console.log(negativeInfinity / 0...
While exploring objects in JavaScript, you have probably noticed that an object is created with the keyword new.Let’s take a look at an example:Javascript create object with new keyword1 2 3 4 5 function MyFunction() { this.val = 100; } let obj = new MyFunction(); console.log(obj...
constmyObj={greet:'Hello',func:functionsayHello(){console.log(this.greet)},}myObj.func()// Hello JavaScript We have an object that contains a property that holds a function. This property is also known as a method. Whenever this method is called, itsthiskeyword will be bound to its imm...
empObj1.salary=30000;console.log(empObj1.salary);// 15varempObj2 =newEmployee();console.log(empObj2.salary);// undefined As we see in the above example, the salary variable adds to theempObj1. But when we try to access the salary variable using theempObj2object, it doesn't have ...
console.log(!!navigator.userAgent.match(/MSIE 8.0/)); // returns either true or false 1. 2. It converts a nonboolean to an inverted boolean (for instance, !5 would be false, since 5 is a non-false value in JS), then boolean-inverts that so you get the original value as a bool...
Below is an example for the !! (not not) operator in JavaScript ? Open Compiler console.log(!!"Hello");console.log(!!0);console.log(!!null);console.log(!![]);console.log(!!{}); Output true false false true true Example
A callback function in JavaScript is a function that is passed as an argument to another function and is invoked after some kind of event.
//方式一.使用thisinvoker.whoInvokeMe=function(){console.log(this.name); }//方式二.不使用thisfunctionwhoInvokeMe2(invoker){console.log(invoker.name); } 方式二的方式并不是语法错误,可以让开发者避开了因为对this关键字的误用而引发的混乱,同样也避开了this所带来的对代码的抽象能力和简洁性,同时会造成...