您的位置:首页 > Web前端 > JavaScript

JavaScript面向对象(2)-一些属性、方法、运算符

2016-12-15 18:50 447 查看
1.hasOwnProperty()

每个实例对象都有一个hasOwnProperty()方法,用来判断某一个属性到底是本地属性,还是继承自prototype对象的属性。

<script type="text/javascript">
function CreatePerson(name){
this.name = name;
}

CreatePerson.prototype.type="men";
CreatePerson.prototype.showName = function(){
alert( this.name );
};

var p1 = new CreatePerson('小明');

alert( p1.hasOwnProperty("name") );  //true  本地属性
alert( p1.hasOwnProperty("type") );  //false  继承自prototype对象的属性

</script>


2.constructor

每个对象在创建时都自动拥有一个构造函数属性constructor,其中包含了一个指向其构造函数的引用。而这个constructor属性实际上继承自原型对象,而constructor也是原型对象唯一的自有属性。

<script type="text/javascript">
function CreatePerson(name){
this.name = name;
}

CreatePerson.prototype.type="men";
CreatePerson.prototype.showName = function(){
alert( this.name );
};

var p1 = new CreatePerson('小明');

alert( CreatePerson.prototype.constructor==CreatePerson );  //true
alert( p1.constructor==CreatePerson );  //false

</script>


3.instanceof 运算符

对象与构造函数在原型链上是否有关系

<script type="text/javascript">
function CreatePerson(name){
this.name = name;
}

CreatePerson.prototype.type="men";
CreatePerson.prototype.showName = function(){
alert( this.name );
};

var p1 = new CreatePerson('小明');

alert( p1 instanceof CreatePerson ); //true
alert( p1 instanceof Object );  //true

</script>


4.toString()

系统对象下面都是自带的 , 自己写的对象都是通过原型链找object下面的。

<script type="text/javascript">
function CreatePerson(name){
this.name = name;
}

CreatePerson.prototype.type="men";
CreatePerson.prototype.showName = function(){
alert( this.name );
};

var p1 = new CreatePerson('小明');
var a1 = new Array();

alert( p1.toString==Object.prototype.toString ); //true
alert( a1.toString==Object.prototype.toString );  //false
alert( a1.toString==Array.prototype.toString );  //true
</script>


toString() : 把对象转成字符串

var num=255;
alert(num.toString(16));  //'ff'  把数字转成十六进制对应的字符串


利用toString做类型的判断 (比如:判断一个对象是不是数组):

var arr = [];
alert( Object.prototype.toString.call(arr) == '[object Array]' ); //true


利用 constructor 和 instanceof 也可以做判断,但是特殊情况下会出现错误:

var oF = document.createElement('iframe');
document.body.appendChild( oF );

var ifArray = window.frames[0].Array;

var arr = new ifArray();

alert( arr.constructor == Array );  //false
alert( arr instanceof Array );  //false
alert( Object.prototype.toString.call(arr) == '[object Array]' );  //true


5.isPrototypeOf()

用来判断某个原型和某个实例之间的关系。

<script type="text/javascript">
function CreatePerson(name){
this
4000
.name = name;
}
CreatePerson.prototype.type="men";
CreatePerson.prototype.showName = function(){
alert( this.name );
};
var p1 = new CreatePerson('小明');
alert( CreatePerson.prototype.isPrototypeOf(p1) ); //true
</script>


6.in运算符

in运算符可以用来判断,某个实例是否含有某个属性,不管是不是本地属性。

<script type="text/javascript">
function CreatePerson(name){
this.name = name;
}

CreatePerson.prototype.type="men";
CreatePerson.prototype.showName = function(){
alert( this.name );
};

var p1 = new CreatePerson('小明');

alert( "name" in p1 ); //true
alert( "type" in p1 ); //true

</script>


in运算符还可以用来遍历某个对象的属性(除constructor)和方法。

<script type="text/javascript">
function CreatePerson(name){
this.name = name;
}

CreatePerson.prototype.type="men";
CreatePerson.prototype.showName = function(){
alert( this.name );
};

var p1 = new CreatePerson('小明');

for (var prop in p1) {
alert( 'p1['+ prop +']='+p1[prop] );
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息