JavaScript:如何从没有 `return` 的方法中检索值
JavaScript: how to retrieve value from a method that doesn't have `return`
我正在尝试修改现有的日期选择器插件 (Pikaday) 以在某些呈现的 HTML(在插件中呈现)中显示所选日期。
我已经确定下面的 setDate method/function return 是我想使用的日期字符串,但我不确定如何提取它并在我的函数中使用它。
setDate 方法显示为:
setDate: function(date, preventOnSelect)
{
if (!date) {
this._d = null;
if (this._o.field) {
this._o.field.value = '';
fireEvent(this._o.field, 'change', { firedBy: this });
}
return this.draw();
}
if (typeof date === 'string') {
date = new Date(Date.parse(date));
}
if (!isDate(date)) {
return;
}
var min = this._o.minDate,
max = this._o.maxDate;
if (isDate(min) && date < min) {
date = min;
} else if (isDate(max) && date > max) {
date = max;
}
this._d = new Date(date.getTime());
setToStartOfDay(this._d);
this.gotoDate(this._d);
if (this._o.field) {
this._o.field.value = this.toString();
fireEvent(this._o.field, 'change', { firedBy: this });
//I want to return `this._o.field.value`
}
if (!preventOnSelect && typeof this._o.onSelect === 'function') {
this._o.onSelect.call(this, this.getDate());
}
}
我想 return this._o.field.value
.
有人知道我如何获取它并在我的函数中使用它吗?
PS,我来自 PHP OOP 背景,所以我可能缺乏对 JS OOP 的理解,抱歉。我的 HTML 渲染函数:
renderHeading = function(instance, c, year, month, days)
{
var opts = instance._o;
var html = '<div class="pika-heading">';
html += '<h3 class="pika-heading-year">' + year + '</h3>';
html += '<h1 class="pika-heading-date">' + opts.i18n.weekdaysShort[day] + ', ' + opts.i18n.monthsShort[month] + '</h1>';
return html += '</div>';
}
if (this._o.field) {
this._o.field.value = this.toString();
window.myDateProperty = this._o.field.value;// Add your own property
fireEvent(this._o.field, 'change', { firedBy: this });
//I want to return `this._o.field.value`-- I wont prefer returning as that alters the library code.
}
现在 window.myDateProperty
包含 this._o.field.value
将在 javascript 文件中可用。
PS:总是做检查:
if(window.myDateProperty){
//use window.myDateProperty
}
我正在尝试修改现有的日期选择器插件 (Pikaday) 以在某些呈现的 HTML(在插件中呈现)中显示所选日期。
我已经确定下面的 setDate method/function return 是我想使用的日期字符串,但我不确定如何提取它并在我的函数中使用它。
setDate 方法显示为:
setDate: function(date, preventOnSelect)
{
if (!date) {
this._d = null;
if (this._o.field) {
this._o.field.value = '';
fireEvent(this._o.field, 'change', { firedBy: this });
}
return this.draw();
}
if (typeof date === 'string') {
date = new Date(Date.parse(date));
}
if (!isDate(date)) {
return;
}
var min = this._o.minDate,
max = this._o.maxDate;
if (isDate(min) && date < min) {
date = min;
} else if (isDate(max) && date > max) {
date = max;
}
this._d = new Date(date.getTime());
setToStartOfDay(this._d);
this.gotoDate(this._d);
if (this._o.field) {
this._o.field.value = this.toString();
fireEvent(this._o.field, 'change', { firedBy: this });
//I want to return `this._o.field.value`
}
if (!preventOnSelect && typeof this._o.onSelect === 'function') {
this._o.onSelect.call(this, this.getDate());
}
}
我想 return this._o.field.value
.
有人知道我如何获取它并在我的函数中使用它吗?
PS,我来自 PHP OOP 背景,所以我可能缺乏对 JS OOP 的理解,抱歉。我的 HTML 渲染函数:
renderHeading = function(instance, c, year, month, days)
{
var opts = instance._o;
var html = '<div class="pika-heading">';
html += '<h3 class="pika-heading-year">' + year + '</h3>';
html += '<h1 class="pika-heading-date">' + opts.i18n.weekdaysShort[day] + ', ' + opts.i18n.monthsShort[month] + '</h1>';
return html += '</div>';
}
if (this._o.field) {
this._o.field.value = this.toString();
window.myDateProperty = this._o.field.value;// Add your own property
fireEvent(this._o.field, 'change', { firedBy: this });
//I want to return `this._o.field.value`-- I wont prefer returning as that alters the library code.
}
现在 window.myDateProperty
包含 this._o.field.value
将在 javascript 文件中可用。
PS:总是做检查:
if(window.myDateProperty){
//use window.myDateProperty
}