Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Professional JavaScript for Web Developers 3rd edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers 3rd edition
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 3rd Edition by Nicholas C Zakas; ISBN: 978-1-1180-2669-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 3rd edition section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 14th, 2016, 03:47 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How does the return function in createComparisonFunction() get it's parameters?

On line 4 a function is declared which is RETURNED to the calling function.

How do Object1 and 2 get defined/set?

This is from Chapter 5 of Professional javascript... 3rd edition.


Code:
 <script type="text/javascript">
            function createComparisonFunction(propertyName) {
            
                return function(object1, object2){
                    var value1 = object1[propertyName]; //how do Object1 and object2 get set?
                    var value2 = object2[propertyName];
            
                    if (value1 < value2){
                        return -1;
                    } else if (value1 > value2){
                        return 1;
                    } else {
                        return 0;
                    }
                };
            }
    
            var data = [{name: "Zachary", age: 28}, {name: "Nicholas", age: 29}];
            
            data.sort(createComparisonFunction("name"));
            alert(data[0].name);  //Nicholas
            
            data.sort(createComparisonFunction("age"));
            alert(data[0].name);  //Zachary        
    
        </script>
Reply With Quote
  #2 (permalink)  
Old February 15th, 2016, 09:01 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default .sort accepts a Function() as a parameter

See specs on array.sort()

That sort function should have two object parameters (a,b below) which (I assume) just get "magically"* passed to that function by the .sort function.


As touched on already, array.sort() accepts an optional parameter in the form of a function reference (lets call it sortfunction). The format of this function looks like this:
array.sort(sortfunction)

function sortfunction(a, b){
//Compare "a" and "b" in some fashion, and return -1, 0, or 1
}


* I'm new to .js And:
Any sufficiently advanced technology is indistinguishable from magic.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
"Return parameters" on a form carlf01 BOOK: Expert One-on-One Access Application Development 0 March 2nd, 2007 07:35 PM
function return 'Nothing' problem srinuRocks ASP.NET 2.0 Basics 0 March 10th, 2005 10:32 AM
How to return array value from a function nebpro BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 September 29th, 2004 01:14 PM
how to get a function to return a recordset object lcsgeek Classic ASP Databases 3 September 22nd, 2004 04:30 PM
Return a UDT from a function alex_read Pro VB 6 1 April 23rd, 2004 06:37 AM



All times are GMT -4. The time now is 04:33 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.