delete in JavaScript

The delete operator removes a property from an object. A property should be configurable for delete to remove it.

Object.defineProperty(window, 'myname', {
configurable: true
});

console.log(delete myname); // returns true

delete keyword return a boolean value, indicates the status of the operation.
We cannot delete anything declared with var as it creates non configurable property.

var a = "can't delete";
console.log(delete a); // prints false
console.log(delete aPropertyNotDefinedYet); // prints true

A variable injected into global scope because of not using var will be a configurable property, thus it can be deleted.

When a property is marked as non-configurable, delete won’t have any effect, and will return false in non strict mode. In strict mode this will raise a SyntaxError.

var, let and const create non-configurable properties that cannot be deleted with the delete operator:

We can use delete keyword in strict mode too.

'use strict';
var a = "can't delete";
console.log(delete a); // prints false
a=undefined;
console.log(a); // prints undefined
console.log(b); // prints error as b not defined. 

delete behaves differently in eval() context.

eval('var a = "cat";' +
'console.log(window.a);' +  // prints "cat"
'console.log(delete a);' +  // prints true (?!)
'console.log(window.a);' +  // prints undefined
'console.log(a);');         // Error: a is not defined

When we delete an array element, the array length is not affected but item will be removed from the array.

var trees = ["redwood","bay","cedar","oak","maple"];
delete trees[3];
// this will change the array to ["redwood","bay","cedar","maple"] with length still 5 

Referance

Advertisements
Posted in JavaScript | Leave a comment

Backup all the environment variables in a windows machine

Open command prompt and run bellow command.

C:\> SET >> envronment.txt

This logs all variables in the text file. This will log both user and system variables together. But it is a simple solution for a quick backup.

Posted in Technical | Leave a comment

Solution for FizzBuzz problem.

A solution for FizzBuzz in C# 

Image

Posted in Technical | Tagged | Leave a comment

Program to print even/odd numbers without an if condition.

Print even/odd in minimum lines my favorite solution is.

Image

 

 

 

Posted in Technical | Tagged | 1 Comment

C# – Testing some language features

Optional parameters should be placed at the end in the parameter definition. (Optional parameters is a VS 2010 feature)

Image

We can’t declare a class as private or protected. Only members within a class can have these access modifies.

Image

Automatically implemented properties must have both ‘get’ and ‘set’ accessors. We have to declare a member variable to implement get/set only feature.

Image

We could make set/get alone as private ( 2008 feature )

Image

If you have a property named MyPropery then you can’t have a function with name get_ MyPropery/set_MyProperty(int value) in the class, Hope you know the reason.

Image

Posted in C#, Technical | Tagged | Leave a comment

An example to illustrate Javascript memory leak via closure.

An example for circular reference got created due to closure.

Image

Below diagram may help to understand the problem well.

Image

And the possible solutions for this problem are.

Solution 1 – By breaking circular reference.

Image

Solution 2 – By adding another closure.

Image

The anotherInnerFunction() creates a new closure and executes immediately. The obj reference is created with in this function and it’s scope got over when function finishes. In this solution ‘obj’ is created outside the closure of event function.

Solution 3 – By avoiding closure altogether.

Image

My favorite solution is #3 as this is a natural solution (no explanation needed); we could avoid landing up these types of problems by adding this rule to the coding standard.

I shamelessly copied the samples from this article.

Posted in JavaScript, Technical | Tagged | Leave a comment

pixlr.com – A Handy Image modification website

pixlr.com is a handy online tool to manipulate images.

Posted in Technical | Tagged | Leave a comment