Arrays and functions in JQuery

Posted on 2013-12-16
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 400 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.
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

631 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