對象的方法 {}

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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,677評論 0 4
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • 從《高級程序設(shè)計》中整理出來的數(shù)組的概念和數(shù)組對象的一些方法,分享給大家同時也是復(fù)習(xí)復(fù)習(xí),有不正確的地方歡迎指正,...
    島民小強閱讀 489評論 0 8
  • 函數(shù)和對象 1、函數(shù) 1.1 函數(shù)概述 函數(shù)對于任何一門語言來說都是核心的概念。通過函數(shù)可以封裝任意多條語句,而且...
    道無虛閱讀 4,947評論 0 5
  • 當(dāng)愛已成往事請不要強留相殘 當(dāng)愛已成往事請不要中傷昨日的愛 當(dāng)愛已成往事請讓那回憶的美好依然 往事的點滴里沒有欺騙...
    溪月伊人閱讀 375評論 0 0

友情鏈接更多精彩內(nèi)容