JS 中 new 操作符

来源:互联网 发布:百度云 域名注册查询 编辑:程序博客网 时间:2024/04/27 21:34

按照 javascript 语言精粹中所说,如果在一个函数前面带上 new 来调用该函数,那么将创建一个隐藏连接到该函数的 prototype 成员的新对象,同时 this 将被绑定到那个新对象上。这个话很抽象,我想用实例来让自己加深理解。

1. 如果就一个函数,没有返回值,没有 prototype 成员,然后使用 new, 会是什么结果呢?如果一个函数没有返回值,那么如果不使用 new 来创建变量,那么该变量的值为 undefined. 如果用了 new,那么就是 Object. 说明一个函数的默认的 Prototype 是 Object.


function Test1(str) {    this.a = str;}var myTest = new Test1("test1");alert(myTest); //[object Object]function Test1WithoutNew(str) {    this.a = str;}var myTestWithoutNew = Test1WithoutNew("test1");alert(myTestWithoutNew); //undefined;

2. 如果函数有返回值,但是返回值是基本类型。那么 new 出来的 myTest 还是 object. 因为基本类型的 prototype 还是 Object. 而如果不使用 new,那么返回值就是 string 的值。


function Test1(str) {    this.a = str;    return this.a;}var myTest = new Test1("test1");alert(myTest); //Objectfunction Test1WithoutNew(str) {    this.a = str;    return this.a;}var myTestWithoutNew = Test1WithoutNew("test1");alert(myTestWithoutNew); //"test1"

3。如果函数的返回值为 new 出来的对象,那么 myTest 的值根据 new 出来的对象的 prototype 而定。

function Test1(str) {    this.a = str;    return new String(this.a);}var myTest = new Test1("test1");alert(myTest); //String "test1"

4。接下来我们开始讨论 new 中的 this。如果我们给 Test1 的 prototype 中加入一个方法叫 get_string(), 那么 get_string() 中的 this 指的就是这个新对象。能够得到在 new 时候赋予该对象的属性值。


var Test2 = function(str) {    this.a = str;}Test2.prototype.get_string = function () {    return this.a;};var myTest2 = new Test2("test2");alert(myTest2.get_string()); //“test2”var Test2 = function(str) {    this.a = str;}Test2.prototype.get_string = function () {    return this.a;};var myTest2 = Test2("test2");alert(myTest2)//undefined

5。如果我们修改了函数的 prototype, 又会发生什么样的情况呢? 那么就会发生类似继承的功能,其实就是 js 的伪类实现。


function Test1(str) {    this.b = str;}Test1.prototype.Get_Test1String = function () {    return this.b;};var Test2 = function(str) {    this.a = str;}Test2.prototype = new Test1("test1");Test2.prototype.get_string = function () {    return this.a;};var myTest2 = new Test2("test2");alert(myTest2); //Objectalert(myTest2.get_string()); //"test2"alert(myTest2.Get_Test1String()); //"test1"

JS 中 new 操作符

Posted on 2011-10-12 17:39 ritazhou 阅读 (12621) 评论 (9) 编辑 收藏

按照 javascript 语言精粹中所说,如果在一个函数前面带上 new 来调用该函数,那么将创建一个隐藏连接到该函数的 prototype 成员的新对象,同时 this 将被绑定到那个新对象上。这个话很抽象,我想用实例来让自己加深理解。

1. 如果就一个函数,没有返回值,没有 prototype 成员,然后使用 new, 会是什么结果呢?如果一个函数没有返回值,那么如果不使用 new 来创建变量,那么该变量的值为 undefined. 如果用了 new,那么就是 Object. 说明一个函数的默认的 Prototype 是 Object.


function Test1(str) {    this.a = str;}var myTest = new Test1("test1");alert(myTest); //[object Object]function Test1WithoutNew(str) {    this.a = str;}var myTestWithoutNew = Test1WithoutNew("test1");alert(myTestWithoutNew); //undefined;

2. 如果函数有返回值,但是返回值是基本类型。那么 new 出来的 myTest 还是 object. 因为基本类型的 prototype 还是 Object. 而如果不使用 new,那么返回值就是 string 的值。


function Test1(str) {    this.a = str;    return this.a;}var myTest = new Test1("test1");alert(myTest); //Objectfunction Test1WithoutNew(str) {    this.a = str;    return this.a;}var myTestWithoutNew = Test1WithoutNew("test1");alert(myTestWithoutNew); //"test1"

3。如果函数的返回值为 new 出来的对象,那么 myTest 的值根据 new 出来的对象的 prototype 而定。

function Test1(str) {    this.a = str;    return new String(this.a);}var myTest = new Test1("test1");alert(myTest); //String "test1"

4。接下来我们开始讨论 new 中的 this。如果我们给 Test1 的 prototype 中加入一个方法叫 get_string(), 那么 get_string() 中的 this 指的就是这个新对象。能够得到在 new 时候赋予该对象的属性值。


var Test2 = function(str) {    this.a = str;}Test2.prototype.get_string = function () {    return this.a;};var myTest2 = new Test2("test2");alert(myTest2.get_string()); //“test2”var Test2 = function(str) {    this.a = str;}Test2.prototype.get_string = function () {    return this.a;};var myTest2 = Test2("test2");alert(myTest2)//undefined

5。如果我们修改了函数的 prototype, 又会发生什么样的情况呢? 那么就会发生类似继承的功能,其实就是 js 的伪类实现。


function Test1(str) {    this.b = str;}Test1.prototype.Get_Test1String = function () {    return this.b;};var Test2 = function(str) {    this.a = str;}Test2.prototype = new Test1("test1");Test2.prototype.get_string = function () {    return this.a;};var myTest2 = new Test2("test2");alert(myTest2); //Objectalert(myTest2.get_string()); //"test2"alert(myTest2.Get_Test1String()); //"test1"

JS 中 new 操作符

Posted on 2011-10-12 17:39 ritazhou 阅读 (12621) 评论 (9) 编辑 收藏

按照 javascript 语言精粹中所说,如果在一个函数前面带上 new 来调用该函数,那么将创建一个隐藏连接到该函数的 prototype 成员的新对象,同时 this 将被绑定到那个新对象上。这个话很抽象,我想用实例来让自己加深理解。

1. 如果就一个函数,没有返回值,没有 prototype 成员,然后使用 new, 会是什么结果呢?如果一个函数没有返回值,那么如果不使用 new 来创建变量,那么该变量的值为 undefined. 如果用了 new,那么就是 Object. 说明一个函数的默认的 Prototype 是 Object.


function Test1(str) {    this.a = str;}var myTest = new Test1("test1");alert(myTest); //[object Object]function Test1WithoutNew(str) {    this.a = str;}var myTestWithoutNew = Test1WithoutNew("test1");alert(myTestWithoutNew); //undefined;

2. 如果函数有返回值,但是返回值是基本类型。那么 new 出来的 myTest 还是 object. 因为基本类型的 prototype 还是 Object. 而如果不使用 new,那么返回值就是 string 的值。


function Test1(str) {    this.a = str;    return this.a;}var myTest = new Test1("test1");alert(myTest); //Objectfunction Test1WithoutNew(str) {    this.a = str;    return this.a;}var myTestWithoutNew = Test1WithoutNew("test1");alert(myTestWithoutNew); //"test1"

3。如果函数的返回值为 new 出来的对象,那么 myTest 的值根据 new 出来的对象的 prototype 而定。

function Test1(str) {    this.a = str;    return new String(this.a);}var myTest = new Test1("test1");alert(myTest); //String "test1"

4。接下来我们开始讨论 new 中的 this。如果我们给 Test1 的 prototype 中加入一个方法叫 get_string(), 那么 get_string() 中的 this 指的就是这个新对象。能够得到在 new 时候赋予该对象的属性值。


var Test2 = function(str) {    this.a = str;}Test2.prototype.get_string = function () {    return this.a;};var myTest2 = new Test2("test2");alert(myTest2.get_string()); //“test2”var Test2 = function(str) {    this.a = str;}Test2.prototype.get_string = function () {    return this.a;};var myTest2 = Test2("test2");alert(myTest2)//undefined

5。如果我们修改了函数的 prototype, 又会发生什么样的情况呢? 那么就会发生类似继承的功能,其实就是 js 的伪类实现。


function Test1(str) {    this.b = str;}Test1.prototype.Get_Test1String = function () {    return this.b;};var Test2 = function(str) {    this.a = str;}Test2.prototype = new Test1("test1");Test2.prototype.get_string = function () {    return this.a;};var myTest2 = new Test2("test2");alert(myTest2); //Objectalert(myTest2.get_string()); //"test2"alert(myTest2.Get_Test1String()); //"test1"