Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Professional JavaScript for Web Developers 3rd edition
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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>
  #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.


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 12:14 PM
how to get a function to return a recordset object lcsgeek Classic ASP Databases 3 September 22nd, 2004 03:30 PM
Return a UDT from a function alex_read Pro VB 6 1 April 23rd, 2004 05:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.