defineProperty method in Javascript

defineProperty is a method on Object which allow you to configure the properties to meet some criterias.
Here is a simple example with an employee object with two properties firstName & lastName and append the two properties by overriding the toString method on the object.
You will get Output as : Jameel Moideen

I am going to change the same code by using defineProperty on the object

The first parameter is the name of the object and then second parameter is name of the property we are adding , in our case it’s toString and then the last parameter is json object which have a value going to be a function and three parameters writable,enumerable and configurable.Right now I just declared everything as true.

If u run the example you will get Output as : Jameel Moideen

Let’s understand why we need the three properties such as writable,enumerable and configurable.
writable
One of the very annoying part of the javascript is , if you change the toString property to something else for example

if run this again , everything gets breaks
Let’s change writable to false. If run the same again you will get the correct output as ‘Jameel Moideen’ . This property will prevent overwrite this property later.
enumerable
if you print all the keys inside the object , you can see all the properties including toString.
console.log(Object.keys(employee));
if you set enumerable to false , you can hide toString property from everybody else. If run this again you will get firstName,lastName
configurable

if someone later redefined the object on later for example enumerable to true and run it. You can see toString property came again.


we no need this behavior , so you can restrict this by set configurable to false.

Your First Task using Gulp in ASP.NET MVC Project

Introduction to Gulp

I am going to create the first Gulp task in an ASP.NET MVC project. Go to Node.Js Command Prompt

Install Gulp as a global package

1

Create a package.json file on your project

3

Also Install Gulp locally after  navigate in to the project directory

2

Create a gulpfile.js on your project
In this file, you can load your packages and plugins for the tasks

Open your gulpfile.js and create your first task

4

Run your Gulp task

5

Introduction to Gulp.js

Gulp is a Javascript Task runner. When we are creating Javascript, Html and CSS there’s a lot of little things that we have to do to basically prepare our code either running in a development environment or in production environment.For example minifying and concatenating , adding vendor prefixes, Less to CSS Compilation, Injecting Files Into HTML, Angular Template Cache, Code Analysis and more to our code.

Main API’s In Gulp

  1. gulp.task
  2. gulp.src
  3. gulp.dest
  4. gulp.watch

Installing Gulp

Gulp is created by using Node.js. So you have to install Node.js first , after that we can install gulp and gulp packages using NPM

You can download Node.js from here

In order to get start with Gulp , You have to install it Globally and then install dependent packages and then code your task.

gulp

Create your first Gulp Task

JavaScript date serialization error in WCF POST Request

I have try to send an Ajax Post request from my client. So i am trying to send the DateTime format from client is like ’12/29/2014′ but my server accepts C# datetime. So i got an error like

There was an error deserializing the object of type ‘type’. DateTime content ’12/29/2014′ does not start with ‘\/Date(‘ and end with ‘)\/’ as required for JSON

How to resolve this error?

Write a javascript function like below
function ConvertToJsonDate(date) {
var convertedDate = new Date(date);
var str = Date.UTC(convertedDate.getFullYear(), convertedDate.getMonth(), convertedDate.getDate(), convertedDate.getHours(), convertedDate.getMinutes(), convertedDate.getSeconds(), 0);
var formattedDate = "\/Date(" + str + ")\/";
return formattedDate;
}

Convert your current date before sending request
var params = {
EndDate: ConvertToJsonDate(endDate),
};

Enjoy coding…

Alternative string concatenation of a two string in Javascript

function GetString(string1,string2) {
var str1 = string1.split('');
var str2 = string2.split('');

var finalString = "";
var length = str1.length > str2.length ? str2.length : str1.length;
for (var i = 0; i str2.length) {
var difference = str1.length - str2.length;
for (var i = 0; i < difference; i++) {
var rem = str1[str2.length + i];
finalString = finalString + rem;
}

} else {
var difference = str2.length - str1.length;
for (var i = 0; i < difference; i++) {
var rem = str2[str1.length + i];
finalString = finalString + rem;
}
}
return finalString;
}

var result= GetString(“INDIA”, “USA”);
Output : IUNSDAIA