JS & jQuery

1. How many type of Objects and Types in Java Script?

There are 4 types of Object as below.

Intrinsic objects   - Date ,String .number,.....
Host Objects - Window ,document
Creating Objects -  var obj =new Object();
Activex Object


The type of all objects created with a custom constructor is object. There are only six types in JavaScript:
object, function, string, number, boolean, and undefined.

2. How will you send a model from jQuery to C# model?

   $("#updateUserPreference").click(function () {
        var userLocale = $("#ddlUserLocale").val();
        var userTimeZone = $("#ddlUserTimeZone").val();
       
       var  userPreferenceModel = {
            LocaleCode: userLocale,
            TimeZoneId: userTimeZone
       };
       var Id = $("#userId").val();
        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',  // not datatype:json
            url: updateUserPreferences,
            cache: false,
            data:
                JSON.stringify({
                    userPreferenceModel:userPreferenceModel,
                    userId: Id,
                    localeCode: $("#ddlUserLocale").val()
            }),
            success: function (response) {
                var objectData = vkbeautify.json(response);
                $("#output").html(objectData);
                if (successFx) {
                    successFx(response);
                }
            },
            error: function (jqXHR, textStatus) {
                $("#session-error").html("Request failed: " + textStatus).show();
                $("#output").html("Failed.");
            }
        });
    });

3. What is Object and in how many ways can you create object?
In JavaScript, almost "everything" is an object.
  • Booleans can be objects (or primitive data treated as objects)
  • Numbers can be objects (or primitive data treated as objects)
  • Strings can be objects (or primitive data treated as objects)
  • Dates are always objects
  • Maths are always objects
  • Regular expressions are always objects
  • Arrays are always objects
  • Functions are always objects
  • Objects are objects
There are many ways to create objects.
var book = {
    price: somePrice * discount,
    pages: 500,
    pricePerPage: this.price / this.pages
};
using new keyword.

var person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;

using an Object Constructor


function person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
var myFather = new person("John""Doe"50"blue");

In Java Script Objects are mutable: They are addressed by reference, not by value.
JavaScript variables are not mutable. Only JavaScript objects.

4. Regex to valid an Email Address.


<input id="forwardMailTo" style="width: 80%;"/>

            <span class="field-validation-error field-validation-valid" id="forwardMailToError" style="margin-left:12%;">EmailErrorMsg</span>

var emailRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    $('#btnSend').click(function () {
        if (emailRegex.test($("#forwardMailTo").val())) {
            ResendOrForwardEmail(true);
            if (!$("#forwardMailToError").hasClass("field-validation-valid")) {
                $("#forwardMailToError").addClass("field-validation-valid");
            }
        }
        else {
            $("#forwardMailToError").removeClass("field-validation-valid");
        }

    });

5. what is dataType and contentType in jquery Ajax calls
  • contentType is the header sent to the server, specifying a particular format.
    • Example: I'm sending json or XML
  • dataType is you telling jQuery what kind of response to expect.
    • Expecting JSON, or XML, or HTML, etc....the default it for jQuery to try and figure it out.



No comments:

Post a Comment