美文网首页
对象的方法 {}

对象的方法 {}

作者: 我是走A牧 | 来源:发表于2018-01-05 10:41 被阅读0次

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

相关文章

  • 学习途中之类方法和对象方法

    对象方法1、 对象方法/实例方法我们之前学习的方法就叫做对象方法.对象方法的调用必须要先创建对象,通过对象来调用....

  • 1.8. 对象方法的声明和实现

    目录 对象方法声明 对象方法实现 1.对象方法声明 格式 特征对象方法以-开头如 -(void)xx;对象方法只能...

  • OC +方法和-方法(补充篇)

    类方法和对象方法1、对象方法:① 减号 - 开头;② 只能由对象来调用;③ 对象方法中能访问当前对象的成员变量(实...

  • OC语言基础

    第一课 1.对象方法和类方法区别 对象方法对象方法是属于对象的以减号-开头只能让对象调用,没有对象,这个方法根本不...

  • 数组&字符串方法&Math&Date

    数组方法 String 对象方法 数学Math方法 日期对象Date方法 Number对象方法 function对...

  • 「Objective-C」类和方法

    1. 类方法和对象方法 对象方法 减号 - 开头 只能由对象来调用 对象方法中能访问当前对象的成员变量(实例变量)...

  • Aspects 源码分析

    需求 hook 实例对象的方法(仅该对象的方法被hook) hook 类对象的方法(该类所有对象调用该方法的时候,...

  • 1.1.1. 函数与方法对比

    目录 对象方法 函数 对象方法:(1)对象方法的实现只能写在@implementation...@end中,对象方...

  • OC中的class方法

    实例对象的class方法 实例对象的class方法返回的是该实例对象的类对象 类对象的class方法 类对象的cl...

  • Java自学-类和对象 类方法

    Java的类方法和对象方法 类方法: 又叫做静态方法 对象方法: 又叫实例方法,非静态方法 访问一个对象方法,必须...

网友评论

      本文标题:对象的方法 {}

      本文链接:https://www.haomeiwen.com/subject/mvsjnxtx.html