JavaScrip笔记

1 三个等号 “===”  全等号,不需要类型转换座比较的时候使用。

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()));
};

//实例方法

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值