hasOwnProperty()函數(shù)用于指示一個對象自身(不包括原型鏈)是否具有指定名稱的屬性。如果有,返回true,否則返回false。
function Site(){
this.name = "CodePlayer";
this.url = "http://www.365mini.com/";
this.sayHello = function(){
document.writeln("歡迎來到" + this.name);
};
}
var obj = {
engine: "PHP"
,sayHi: function(){
document.writeln("歡迎訪問" + this.url);
}
};
// 使用對象obj覆蓋Site本身的prototype屬性
Site.prototype = obj;
var s = new Site();
document.writeln( s.hasOwnProperty("name") ); // true
document.writeln( s.hasOwnProperty("sayHello") ); // true
// 以下屬性繼承自原型鏈,因此為false
document.writeln( s.hasOwnProperty("engine") ); // false
document.writeln( s.hasOwnProperty("sayHi") ); // false
document.writeln( s.hasOwnProperty("toString") ); // false
// 想要查看對象(包括原型鏈)是否具備指定的屬性,可以使用in操作符
document.writeln( "engine" in s ); // true
document.writeln( "sayHi" in s ); // true
document.writeln( "toString" in s ); // true
isPrototypeOf()函數(shù)用于指示對象是否存在于另一個對象的原型鏈中。如果存在,返回true,否則返回false。
function Site(){
this.name = "CodePlayer";
this.url = "http://www.365mini.com/";
this.sayHello = function(){
document.writeln("歡迎來到" + this.name);
};
}
var s = new Site();
document.writeln( Site.prototype.isPrototypeOf(s) ); // true s必須是個對象
var obj = {
engine: "PHP"
,sayHi: function(){
document.writeln("歡迎訪問" + this.url);
}
};
// 使用對象obj覆蓋Site本身的prototype屬性
Site.prototype = obj;
var s2 = new Site();
document.writeln( obj.isPrototypeOf(s2) ); // true obj在S2的原型鏈中 s2必須是個對象
toLocaleString()函數(shù)用于將當(dāng)前對象以字符串值的形式返回,該字符串的格式適合當(dāng)前宿主環(huán)境的當(dāng)前區(qū)域設(shè)置。
// 數(shù)組
var array = ["CodePlayer", true, 12, -5];
document.writeln( array.toLocaleString() ); // CodePlayer,true,12,-5
// 日期
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
document.writeln( date.toLocaleString() ); // 2013年8月18日 下午11:11:59
// 日期2
var date2 = new Date(1099, 7, 18, 23, 11, 59, 230);
document.writeln( date2.toLocaleString() ); // 1099年8月12日 下午11:17:51
// 數(shù)字
var num = 15.26540;
document.writeln( num.toLocaleString() ); // 15.265
// 布爾
var bool = true;
document.writeln( bool.toLocaleString() ); // true
// Object
var obj = {name: "張三", age: 18};
document.writeln( obj.toLocaleString() ); // [object Object]
toString()函數(shù)用于將當(dāng)前對象以字符串的形式返回。
//數(shù)組
var array = ["CodePlayer", true, 12, -5];
document.writeln( array.toString() ); // CodePlayer,true,12,-5
// 日期
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
document.writeln( date.toString() ); // Sun Aug 18 2013 23:11:59 GMT+0800 (中國標(biāo)準(zhǔn)時間)
// 日期2
var date2 = new Date(1099, 7, 18, 23, 11, 59, 230);
document.writeln( date2.toString() ); // Fri Aug 18 1099 23:11:59 GMT+0800 (中國標(biāo)準(zhǔn)時間)
// 數(shù)字
var num = 15.26540;
document.writeln( num.toString() ); // 15.2654
// 布爾
var bool = true;
document.writeln( bool.toString() ); // true
// Object
var obj = {name: "張三", age: 18};
document.writeln( obj.toString() ); // [object Object]
valueOf()函數(shù)用于返回指定對象的原始值。
// Array:返回數(shù)組對象本身
var array = ["CodePlayer", true, 12, -5];
document.writeln( array.valueOf() === array ); // true
// Date:當(dāng)前時間距1970年1月1日午夜的毫秒數(shù)
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
document.writeln( date.valueOf() ); // 1376838719230
// Number:返回數(shù)字值
var num = 15.26540;
document.writeln( num.valueOf() ); // 15.2654
// 布爾:返回布爾值true或false
var bool = true;
document.writeln( bool.valueOf() === bool ); // true
// new一個Boolean對象
var newBool = new Boolean(true);
// valueOf()返回的是true,兩者的值相等
document.writeln( newBool.valueOf() == newBool ); // true
// 但是不全等,兩者類型不相等,前者是boolean類型,后者是object類型
document.writeln( newBool.valueOf() === newBool ); // false