将数据传递给函数。有什么更好的方法?

时间:2017-07-31 04:55:25

标签: php

方式1:

// with $foo = 1 in other function
function($foo){
    return $foo+1;
}

方式2:

/* Note: I have a set of data, and it is named by the variable data.
 * The variable data is an array
 */

//Here I get the length of the data
var dataLength = data.length;

//Then I iterate through all my pieces of data here
//NOTICE THAT THE KEYWORD let IS SO IMPORTANT HERE
for(let markerIterator = 0; markerIterator < dataLength; markerIterator++) {
    /* This creates a new google maps marker based on my data's latitude and 
     * longitude
     */
    var marker = new google.maps.Marker({
        position: { lat: data[markerIterator].latitude, lng: 
        data[markerIterator].longitude },
        map: map
    });

    google.maps.event.addListener(marker, 'click', function () {
        /* This will spit out the unique markerIterator value for each marker when 
         * clicked. It is a unique value because I defined markerIterator with the 
         * keyword let!
         */
        console.log(markerIterator); 

        // Use the keyword this to refer to each unique marker, for example:
        map.setCenter(this.getPosition());              
    });
}

对于一个'愚蠢'的问题,有人可以告诉我什么是更好的方法?

1 个答案:

答案 0 :(得分:0)

第二种方式更好,它比第一种方式更规范。有关详细信息,请参阅此文档http://php.net/manual/en/functions.arguments.php