「这个」对象创建期间的上下文

发布于 2024-10-02 03:10:14 字数 150 浏览 5 评论 0原文

我正在尝试做这样的事情:

var test = {
    a: 10,
    b: 20,
    c: (this.a+this.b)
};

但它不起作用。如何从 test.c 中访问 test.a? 是否可以?

I am trying to do something like this:

var test = {
    a: 10,
    b: 20,
    c: (this.a+this.b)
};

but it doesn't work. How can I access the test.a from within test.c?
Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自此以后,行同陌路 2024-10-09 03:10:14

不可能在指定对象文字的表达式中引用“this”。要么在下面的行中执行此操作,要么使用如下所示的构造函数:

function myobj(a,b) {
  this.a = a;
  this.b = b;
  this.c = this.a + this.b;
}

var test = new myobj(10,20);

根据哪种方法更快,使用对象构造函数创建更快。这是一个简单的测试用例比较。 在 JSBIN 上自行运行

结果表明,使用构造函数创建对象与使用对象字面量创建对象的速度几乎是对象创建速度的两倍:

0.450s:testObjectLiteral

0.506s:testObjectLiteralWithFunction

0.280s:测试构造函数

这也是内联的测试代码:

// timer function
function time(scope){ 
  time.scope = time.scope || {}; 
  if(time.scope[scope]) {
    var duration = (new Date()).getTime()-time.scope[scope]; 
    time.scope[scope] = null; 
    var results = document.getElementById("results");
    results.innerHTML = results.innerHTML + '<p>'+(duration/1000).toFixed(3)+'s : '+scope+'</p>';
  } else { 
    time.scope[scope] = (new Date()).getTime();
  } 
}  

// object creation function with constructor
function myobj(a,b) {
  this.a = a;
  this.b = b;
  this.c = this.a + this.b;
}

function testConstructor(iterations) {
  var objs = new Array(iterations);
  for(i=0;i<iterations;i++) {
    objs[i] = new myobj(i,i+1);
  }
  return objs;
}

function testObjectLiteralWithFunction(iterations) {
  var objs = new Array(iterations);
  for(i=0;i<iterations;i++) {
    objs[i] = {
      a: i,
      b: i+1,
      c: function() {
        return this.a + this.b;
      }
    };
  }  
  return objs;
}


function testObjectLiteral(iterations) {
  var objs = new Array(iterations);
  for(i=0;i<iterations;i++) {
    var item = {
      a: i,
      b: i+1
    };
    item.c = item.a + item.b;
    objs[i] = item;
  }  
  return objs;
}

var ITERATIONS = 1000000;
time("testObjectLiteral");
testObjectLiteral(ITERATIONS);
time("testObjectLiteral");

time("testObjectLiteralWithFunction");
testObjectLiteralWithFunction(ITERATIONS);
time("testObjectLiteralWithFunction");

time("testConstructor");
testConstructor(ITERATIONS);
time("testConstructor");

It's not possible to reference "this" in an expression specifying an object literal. Either do it in a following line or use a constructor like this:

function myobj(a,b) {
  this.a = a;
  this.b = b;
  this.c = this.a + this.b;
}

var test = new myobj(10,20);

In response to which method is faster, creation with the object constructor is faster. Here's a simple test case comparison. Run it yourself on JSBIN.

The results show that the object creation with a constructor vs an object literal is almost twice as fast:

0.450s : testObjectLiteral

0.506s : testObjectLiteralWithFunction

0.280s : testConstructor

Here's the test code inlined as well:

// timer function
function time(scope){ 
  time.scope = time.scope || {}; 
  if(time.scope[scope]) {
    var duration = (new Date()).getTime()-time.scope[scope]; 
    time.scope[scope] = null; 
    var results = document.getElementById("results");
    results.innerHTML = results.innerHTML + '<p>'+(duration/1000).toFixed(3)+'s : '+scope+'</p>';
  } else { 
    time.scope[scope] = (new Date()).getTime();
  } 
}  

// object creation function with constructor
function myobj(a,b) {
  this.a = a;
  this.b = b;
  this.c = this.a + this.b;
}

function testConstructor(iterations) {
  var objs = new Array(iterations);
  for(i=0;i<iterations;i++) {
    objs[i] = new myobj(i,i+1);
  }
  return objs;
}

function testObjectLiteralWithFunction(iterations) {
  var objs = new Array(iterations);
  for(i=0;i<iterations;i++) {
    objs[i] = {
      a: i,
      b: i+1,
      c: function() {
        return this.a + this.b;
      }
    };
  }  
  return objs;
}


function testObjectLiteral(iterations) {
  var objs = new Array(iterations);
  for(i=0;i<iterations;i++) {
    var item = {
      a: i,
      b: i+1
    };
    item.c = item.a + item.b;
    objs[i] = item;
  }  
  return objs;
}

var ITERATIONS = 1000000;
time("testObjectLiteral");
testObjectLiteral(ITERATIONS);
time("testObjectLiteral");

time("testObjectLiteralWithFunction");
testObjectLiteralWithFunction(ITERATIONS);
time("testObjectLiteralWithFunction");

time("testConstructor");
testConstructor(ITERATIONS);
time("testConstructor");

甜宝宝 2024-10-09 03:10:14

在对象字面量中这是不可能的,因为 this 无法引用尚未创建的对象。最好的选择是在单独的步骤中分配 c 属性:

var test = {
    a: 10,
    b: 20
};

test.c = test.a + test.b;

It's not possible within an object literal since this cannot be made to refer to an object that has not yet been created. Your best option is to assign the c property in a separate step:

var test = {
    a: 10,
    b: 20
};

test.c = test.a + test.b;
小瓶盖 2024-10-09 03:10:14

在声明对象字面量时,您根本无法执行此操作,您可以做的最接近的是:

var test = {
    a: 10,
    b: 20
};
test.c = test.a + test.b;

在您的上下文中 this 指的是您所在的任何父上下文,而不是 test 对象...即使它确实如此,您也不能像这样声明成员,例如这也是无效的:

var test = { a: 10, b: 20, test.c: test.a + test.b };

...因为 testa 和 < code>b 尚未定义,因为它是尚未完成的单个语句。

You simply can't do this when declaring an object literal, the closest you can do is:

var test = {
    a: 10,
    b: 20
};
test.c = test.a + test.b;

In your context this refers to whatever parent context you're in, not the test object...and even if it did, you can't declare members like that, for example this is also invalid:

var test = { a: 10, b: 20, test.c: test.a + test.b };

...because test, a and b aren't defined yet, since it's a single statement that hasn't completed.

我的奇迹 2024-10-09 03:10:14

为什么不让 ca 函数始终返回 a+b 的当前值?

var test = {
    a: 5,
    b: 1,
    c: function() {
        return this.a + this.b;
    }
}

Why not make c a function so that it always returns the current value of a+b?

var test = {
    a: 5,
    b: 1,
    c: function() {
        return this.a + this.b;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文