Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Arrays and functions in JQuery

Posted on 2013-12-16
Medium Priority
Last Modified: 2013-12-18
We are using client side technologies Kendo UI,knockout and Jquery for our application , in one of the pages we have a grid that will list out all the books with book name,author name, rate /price etc with default sort on rate column.I have following piece of JQuery  code in bookModel.js file and am finding difficult to understand  and  can anyone briefly explain  the code . (which commented with question marks)

var bookresult={

// Properties and functions







//some logic

var tempgroup=new Array();

//some logic

[b]//why we need to pass this.get ?[/b]




Open in new window

//function defined

dataModels.BooksearchComparator=function(sortAsc,sortField,defaultSort) {

var bookcomparator=fucntion(x,y) {     // ? 

var valueXRank=a["booksearchRank"];    // ?
var valueYRank=b["booksearchRank"];

if(typeof(a.booksearchRank)=='undefined') {   // ?
var valueX=a[sortField]; //?
var valueY=b[sortField];


return bookcomparator;


Open in new window

Question by:ksd123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39721309
You have an error on that JSON line 5.
Booksearchcriteria property doesn't have a value neither ends with a comma.
So starting with this you'll have javascript errors.

dataModels.BooksearchComparator = function (sortAsc, sortField, defaultSort) {

    var bookcomparator = fucntion(x, y) { // function have a spelling error, should be function, not fucntion

        var valueXRank = a["booksearchRank"]; // get a property value from a json object
        var valueYRank = b["booksearchRank"];

        //if (typeof (a.booksearchRank) == 'undefined') { // this is no a propper way
        // undefined is actually a type in javascript. 
        // It differs from null. 
        //    Null is when the property exists but is not assigned
        //    undefined is when the property doens't even exist
        if (a.booksearchRank === undefined) { // this is correct
            valueXRank = 0;
            valueYRank = 0;
        var valueX = a[sortField]; // again, gets the sortField property value from a. This is the same as a.sortField
        var valueY = b[sortField];


    return bookcomparator;


Open in new window


Author Comment

ID: 39721415
Thank you for the quick response.

1)What is the diff b/w passing params to a function  using  this.get("sortAsc") and just


2)can you briefly explain below two lines of code .It looks like function within a function?

dataModels.BooksearchComparator = function (sortAsc, sortField, defaultSort) {

    var bookcomparator = fucntion(x, y) {
LVL 30

Accepted Solution

Alexandre Simões earned 1200 total points
ID: 39721446
1) I'm a bit lost with that: this.get("sortAsc")
Does that actually work?

To get the value from a JSON object you have 2 options:

this.get("sortAsc") should throw an error unless there's a get() function somewhere.

2) it is in-fact a function within a function.
This is something normal in javascript. Basically we're assigning bookcomparator variable value as a function.

functions in javascript are first-class citizens, (I don't like this comparison but usually makes it easier to explain) much like classes on OOP languages.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 39723586
Thank you.I have two more questions.

1)Below is my bookModel.js file, The first function has new keyword and remaining all functions does not have new keyword. Please comment

2)In the bookModel.js file, I want to declare a variable that will set to boolean value and  should  be available to all the functions in this file, where and How should I declare this?


 dataModels.bookSearch=new function() {  //function
 //No code in this function

 dataModels.bookSearch=function() {         //function

 //code in this function

 dataModels.bookSearch.refreshdata=function() {  //function
 //code in this function


Open in new window

LVL 30

Expert Comment

by:Alexandre Simões
ID: 39723832
I will comment but I think you should read at least these books:

After read this:

And you'll have the knowledge needed to answer all the questions you have now.

I can't answer this right now, give me a couple of hours and I'll be back.


Author Closing Comment

ID: 39726452
Thank you for your time

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to dynamically set the form action using jQuery.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question