JavaScripノート


1の3つの等号「=」  全等号は、タイプ変換席の比較が不要な時に使用します.
eg:「1」==1/true       「1」==1//falseは、データの種類を比較します.
 
2プラス記号(+)で文字列をつなぎ合わせて、IEは性能問題があり、ネットワーク上に流れる解決策は以下の通りである.
/**
 * javaScript       “2”+“3”                  。
 *     StringBuffer 。
 * var buf = new StringBuffer();
 * buf.append("string");
 * */
function StringBuffer() {
this._strings = [];
if (arguments.length == 1) {
this._strings.push(arguments[0]);
}
}

StringBuffer.prototype.append = function(str) {
this._strings.push(str);
return this;
}

StringBuffer.prototype.toString = function() {
return this._strings.join("");
}

StringBuffer.prototype.length = function() {
var str = this._strings.join("");
return str.length;
}

StringBuffer.prototype.del = function(num) {
var len = this.length();
var str = this.toString();
str = str.slice(0, len - num);
this._strings = [];
this._strings.push(str);
}
 
 
3拡張Date:
/** Adds the number of days array to the Date object. */
Date._MD = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

/** Constants used for time computations */
Date.SECOND = 1000 /* milliseconds */;
Date.MINUTE = 60 * Date.SECOND;
Date.HOUR = 60 * Date.MINUTE;
Date.DAY = 24 * Date.HOUR;
Date.WEEK = 7 * Date.DAY;

/**
 *   Date toString       。   var a = new Date(); alert(a.toString("yyyy-MM-dd HH mm ss"));
 */
Date.prototype.toString = function() {
	if (arguments.length > 0) {
		var formatStr = arguments[0];
		var str = formatStr;

		str = str.replace(/yyyy|YYYY/, this.getFullYear());
		str = str.replace(/yy|YY/, (this.getFullYear() % 100) > 9 ? (this
				.getFullYear() % 100).toString() : "0"
				+ (this.getFullYear() % 100));

		str = str.replace(/MM/, this.getMonth() + 1 > 9 ? (this.getMonth() + 1)
				.toString() : "0" + (this.getMonth() + 1));
		str = str.replace(/M/g, (this.getMonth() + 1).toString());

		str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate()
				.toString() : "0" + this.getDate());
		str = str.replace(/d|D/g, this.getDate());

		str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours()
				.toString() : "0" + this.getHours());
		str = str.replace(/h|H/g, this.getHours());

		str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes()
				.toString() : "0" + this.getMinutes());
		str = str.replace(/m/g, this.getMinutes());

		str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds()
				.toString() : "0" + this.getSeconds());
		str = str.replace(/s|S/g, this.getSeconds());

		return str;

	} else {
		return this.toLocaleString();
	}
}

/** Returns the number of days in the current month */
Date.prototype.getMonthDays = function(month) {
	var year = this.getFullYear();
	if (typeof month == "undefined") {
		month = this.getMonth();
	}
	if (((0 == (year % 4)) && ((0 != (year % 100)) || (0 == (year % 400))))
			&& month == 1) {
		return 29;
	} else {
		return Date._MD[month];
	}
};

/** Returns the number of day in the year. */
Date.prototype.getDayOfYear = function() {
	var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0,
			0, 0);
	var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);
	var time = now - then;
	return Math.floor(time / Date.DAY);
};

/** Returns the number of the week in year, as defined in ISO 8601. */
Date.prototype.getWeekNumber = function() {
	var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0,
			0);
	var DoW = d.getDay();
	d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
	var ms = d.valueOf(); // GMT
	d.setMonth(0);
	d.setDate(4); // Thu in Week 1
	return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
};

/** Checks date and time equality */
Date.prototype.equalsTo = function(date) {
	return ((this.getFullYear() == date.getFullYear())
			&& (this.getMonth() == date.getMonth())
			&& (this.getDate() == date.getDate())
			&& (this.getHours() == date.getHours()) && (this.getMinutes() == date
			.getMinutes()));
};

//