处理对象参数
我是 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;
I am new to JavaScript OO programming please bear with me for any ambiguities.
I have a method that requires an object as its arguments.
The required object sets options for the method to work with.
var objInstance = csvObj.getData({
url: '', // csv url
format: '', // return data format
onComplete: function (output) {
// a function that will display data returned
}
});
csvObj is defined below
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
}
}());
}
};
Question: Is there a better way of handling object arguments than simply accessing like this:
var privateProperty = object.property;
or
var privateMethod = object.method;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的方法是完全有效的。您也可以这样做:
这对于动态获取属性非常方便,但总的来说我更喜欢
object.method
,因为它更容易阅读。Your approach is totally valid. You could do it like this, too:
This can be very handy for dynamically getting properties, but in general i prefer
object.method
, because it is much easier to read.