对象的方法 {}

hasOwnProperty()函数用于指示一个对象自身(不包括原型链)是否具有指定名称的属性。如果有,返回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()函数用于指示对象是否存在于另一个对象的原型链中。如果存在,返回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()函数用于将当前对象以字符串值的形式返回,该字符串的格式适合当前宿主环境的当前区域设置。

// 数组
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

// 数字
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()函数用于将当前对象以字符串的形式返回。

//数组
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 (中国标准时间)

// 日期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 (中国标准时间)

// 数字
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()函数用于返回指定对象的原始值。

// Array:返回数组对象本身
var array = ["CodePlayer", true, 12, -5];
document.writeln( array.valueOf() === array ); // true

// Date:当前时间距1970年1月1日午夜的毫秒数
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
document.writeln( date.valueOf() ); // 1376838719230

// Number:返回数字值
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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 8,455评论 0 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,384评论 19 139
  • 从《高级程序设计》中整理出来的数组的概念和数组对象的一些方法,分享给大家同时也是复习复习,有不正确的地方欢迎指正,...
    岛民小强阅读 3,262评论 0 8
  • 函数和对象 1、函数 1.1 函数概述 函数对于任何一门语言来说都是核心的概念。通过函数可以封装任意多条语句,而且...
    道无虚阅读 10,125评论 0 5
  • 当爱已成往事请不要强留相残 当爱已成往事请不要中伤昨日的爱 当爱已成往事请让那回忆的美好依然 往事的点滴里没有欺骗...
    溪月伊人阅读 2,586评论 0 0