Convert JavaScript object To String

21-11-2014
function objToString (obj) {
    var str = '';
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
            str += p + '::' + obj[p] + '\n';
        }
    }
    return str;
}

or

JSON.stringify(obj);

Written with StackEdit.

Read More »

Principal End of One2One Map in EntityFramework

21-11-2014

##What does principal end of an association means?

In one-to-one relation one end must be principal and second end must be dependent. Principal end is the one which will be inserted first and which can exist without the dependent one. Dependent end is the one which must be inserted after the principal because it has foreign key to the principal.

##How to config in FluentAPI? Say we have one to one User and UserProfile entities, and the User is principal, UserProfile is dependant.

class User{
   UserProfile Profile { get;set; }
   //...
}

class UserProfile{
   User User{ get;set; }
   //...
}

Then the fluentAPI configuration will be:

modelBuilder.Entity<User>().HasRequired(u => u.Profile).WithRequiredPrincipal(p => p.User);

Written with StackEdit.

Read More »

The 'interesting' JavaScript Types

20-11-2014

typeof primitive types

Here lists the typeof results of primitive types:

var undefinedVariable;
var nullValue = null;
var anObject = new Object();
var func = function () { };

typeof ("string"); //result in "string"
typeof (1024); // number
typeof (true); // boolean
typeof (undefinedVariable); // undefined
typeof (anObject ); // object
typeof (undeclaredValue); // undefined
typeof (nullValue); // object
typeof (null) // object
typeof (func) // function
typeof (Object) // function

Things need to be highlighted are:

  • Both undefined and undeclared variable are of type undefined
  • Variable is null when assigned with null otherwise it will be undefined
  • Object is a function
  • null is an object, I guess it means a null object.

Read More »

Great JavaScript Interview Questions

20-11-2014

19 Great JavaScript Interview Questions

copied from here

Namespace

What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?

A: This is an increasingly common practice, employed by many popular JavaScript libraries (jQuery, Node.js, etc.). This technique creates a closure around the entire contents of the file which, perhaps most importantly, creates a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries.

Another feature of this technique is to allow for an easily referenceable (presumably shorter) alias for a global variable. This is often used, for example, in jQuery plugins. jQuery allows you to disable the $ reference to the jQuery namespace, using jQuery.noConflict(). If this has been done, your code can still use $ employing this closure technique, as follows:

(function($) { /* jQuery plugin code referencing $ */ } )(jQuery);

Read More »

Get Website URLs via JavaScript

20-11-2014

Copied from here

There are several ways you can do this, but one way might be best for certain situations (e.g. within an iFrame).

Protocol + Domain + Page

document.URL
> "http://example.com/page1.html"

document.location.href
> "http://example.com/page1.html"

Protocol + Domain

document.location.origin
> "http://example.com"

Domain

document.location.host
> "example.com"

Page

document.location.pathname
> "/page1.html"

Written with StackEdit.

Read More »