JS Object类通用的属性与方法


<script type="text/javascript">
// JS通用的Object类属性与方法:
// JavaScript中的所有对象都继承自 Object 类:
// =================================================================================================
// 属性:
// object.constructor属性:JS中,每个对象都有一个constructor属性,它引用了初始化这个对象的构造函数。
// object.prototype属性:对象原型的默认对象引用。在JS中,所有的函数都有一个公共的属性 prototype ,当这个函数被定义时,prototype属性自动建立并被始化,prototype属性值是一个对象,这个对象只有一个属性 constructor,这个 constructor 指回到和原型相关联的那个构造函数。
// 如:
function MySQL(){
this.host;
this.database;
this.user;
this.password;
this.connection = function(){

};
};

var sql = new MySQL();
document.write(sql.constructor); // 输出:function MySQL(){ this.host; this.database; this.user; this.password; this.connection = function(){ }; }
document.write("<br>");
document.write(MySQL.prototype.isPrototypeOf(sql)); // 输出:true
document.write(MySQL.prototype.constructor); // 输出:function MySQL(){ this.host; this.database; this.user; this.password; this.connection = function(){ }; }
document.write("<br>");

var emp = {};
document.write(emp.constructor); // 输出:function Object() { [native code] }
document.write("<br>");
document.write(Object.prototype.isPrototypeOf(emp)); // 输出:true
document.write(Object.prototype.constructor); // 输出:function Object() { [native code] }
document.write("<br>");

var pos = {x:0,y:0};
document.write(pos.constructor); // 输出:function Object() { [native code] }
document.write("<br>");

var obj = new Object();
document.write(obj.constructor); // 输出:function Object() { [native code] }
document.write("<br>");

var num = new Number(2013+10+28);
document.write(num.constructor); // 输出:function Number() { [native code] }
document.write("<br>");

var str = new String("feiesoft.com");
document.write(str.constructor); // 输出:function String() { [native code] }
document.write("<br>");

var arr = new Array(1,2,3);
document.write(arr.constructor); // 输出:function Array() { [native code] }
document.write("<br>");

var boo = new Boolean(1);
document.write(boo.constructor); // 输出:function Boolean() { [native code] }
document.write("<br>");

var now = new Date();
document.write(now.constructor); // 输出:function Date() { [native code] }
document.write("<br>");
document.write(Date.prototype.isPrototypeOf(now)); // 输出:true
document.write(Date.prototype.constructor); // 输出:function Date() { [native code] }
document.write("<br>");

// =================================================================================================
// 方法:
// hasOwnProperty(property):判断对象是否有某个特定的属性。必须用字符串指定该属性(例如,o.hasOwnProperty(”name”))。
// isPrototypeOf(object):判断该对象是否为另一个对象的原型。
// propertyIsEnumerable(property):判断给定的属性是否可以用for…in语句进行枚举。
// toString():返回对象的原始字符串表示。对于Object类,ECMA-262没有定义这个值,所以不同的ECMAScriipt实现具有不同的值。
// valueOf():返回最适合该对象的原值。对于许多类,该方法返回的值都与toString()的返回值相同。

</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值