JavaScript-面向对象-扩展内置对象方法

建立自定义对象的方法

 function Person( ) { }
 Person.prototype={
       constructor:Person,
       name:"Jack",
       age:100,
       sayHi:function ( ) {
             alert("hello"+this.name);
       }
 }; var p1=new Person( );
 p1.sayHi();

扩展内置对象(String)的方法

String.prototype={        constructor:this,        run:function () {
            alert("success!");
        }
    };    var n="####";
    n.run();

后面的constructor属性不指向String对象(前面的constructor属性指向Person对象),这是为什么呢?


互换的青春
浏览 538回答 1
1回答

一只甜甜圈

你前面写了Person,后面写了this
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript