gpt4 book ai didi

javascript - 处理对象参数

转载 作者:行者123 更新时间:2023-12-02 20:33:35 24 4
gpt4 key购买 nike

我是 JavaScript OO 编程新手,如有任何歧义,请耐心等待。

我有一个需要一个对象作为其参数的方法。所需的对象设置要使用的方法的选项。

var objInstance = csvObj.getData({
url: '', // csv url

format: '', // return data format

onComplete: function (output) {
// a function that will display data returned
}
});

csvObj 定义如下

var csvObj = { 
getData: function (o) {

var format = o.format || 'json', //optional options
url = o.url, callback = o.onComplete; // required options

function include_js(jsurl) { // private function for performing cross dormain requests
var script = document.createElement("script");
script.type = "text/javascript";

script.src = jsurl;
document.getElementsByTagName("head")[0].appendChild(script);
}


(function () { // anonymous function for proccessing and returning data

if (typeof callback === 'function' && url) { // if require options are defined

if (!window.call_back) { // and global call_back function is not yet defined
window.call_back = callback; // define global call_back function (will be called when external script is loaded)
}

var restQuery = 'http://query.yahooapis.com/v1/public/yql?q=',
query = 'select * from csv where url="' + url + '" and columns="' + cols + '"',

yqlQuery = restQuery + encodeURIComponent(query) + '&format=' + data + '&callback=call_back'; // create YQL query
include_js(yqlQuery); // create a cross dormain YQL request
}
else {
alert('onComplete function or csv url not defined.'); // required options not defined
}
}());

}
};

问题:有没有比简单访问更好的方法来处理对象参数:

var privateProperty = object.property;

var privateMethod = object.method;

最佳答案

你的方法完全有效。你也可以这样做:

var privateMethod = object['method'];

这对于动态获取属性非常方便,但总的来说我更喜欢object.method,因为它更容易阅读。

关于javascript - 处理对象参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3684241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com