[英]How to define new object of variable type of array object?
jQuery has made my life easier but I'm still very beginner to JavaScript. jQuery使我的生活更轻松,但是我还是JavaScript的初学者。 So, may be, I'm asking a very stupid question here: 所以,可能是,我在这里问一个非常愚蠢的问题:
var t = {
rows: 3,
columns: 5,
getCellCount: function () {
return this.rows * this.columns;
}
};
var tn = t;
tn.rows = 6;
document.write(tn.rows + " , " + t.rows); // returns 6 , 6
I also tried var tn = new t(); // but seems wrong
我也尝试了var tn = new t(); // but seems wrong
var tn = new t(); // but seems wrong
So, How to retrieve old intrinsic value from object so that it results 6 , 3 因此,如何从对象中检索旧的内在值,使其结果为6,3
tn
and t
are both pointing to the same object, that's why when you change tn.rows
it also changes t.rows
. tn
和t
都指向同一个对象,这就是为什么当您更改tn.rows
时也会同时更改t.rows
。 There is no such thing as old intrinsic value
. 没有old intrinsic value
类的东西。
You must copy the object in order to keep the old value. 您必须复制对象以保留旧值。 There are two: shallow copy and deep copy . 有两种: 浅复制和深复制 。
Copying the key-value pairs is pretty easy with Object.create
. 使用Object.create
复制键值对非常容易。
var newObj = Object.create(oldObj);
Now if you change the values in newObj
, it will not change the original one: 现在,如果您更改newObj
的值,则不会更改原始值:
var a = {a:1}, b = Object.create(a);
b.a = 2;
console.log(a.a, b.a); //1,2
However, to perform a complete copy is really complicated. 但是,执行完整复制确实很复杂。 See more: How do I correctly clone a JavaScript object? 查看更多: 如何正确克隆JavaScript对象?
PS: The new
keyword you mentioned is for creating an object as in class
es. PS:您提到的new
关键字是用于像es class
那样创建对象。
function Car(n, y){ //This is called a "constructor"
var name = n, year = y; //Private variables
return {
getName: function(){ return name; }, //Getters
getYear: function(){ return year; }
};
}
var myNewCar = new Car("Toyota", 2010);
myNewCar.getName(); //"Toyota"
myNewCar.getYear(); //2010
(This is also how you create objects in Java, if you have taken those CS courses you would recognize this pattern.) (这也是您在Java中创建对象的方式,如果您修过那些CS课程,您将认识到这种模式。)
var tn = t;
simply makes both tn
and t
to point to the same object in memory. 只需使tn
和t
都指向内存中的同一对象即可。 So, change in one object will reflect in other as well. 因此,一个对象的变化也将反映在另一个对象上。 You can clone the object, like this 您可以像这样克隆对象
function copyObject(sourceObject) {
var result = {};
for (var key in sourceObject) {
if (sourceObject.hasOwnProperty(key)) {
result[key] = sourceObject[key];
}
}
return result;
}
var tn = copyObject(t);
You are asking to clone a JSON object. 您要克隆JSON对象。
var tn={};
for (key in t) tn[key]=t[key];
There may be other "prettier" ways, but this guarantees the clone. 可能还有其他“更漂亮”的方式,但这可以保证克隆。
If you want to create a Javascript object, the conventional way is to create a function: 如果要创建Javascript对象,则常规方法是创建一个函数:
// Use capitalized names for Object-creating functions.
// I guessed that you wanted a matrix of some sort.
function Matrix(rows, columns) {
this.rows = rows;
this.columns = columns;
this.cellCount = function() {
return this.rows * this.columns;
}
this.dimensions = function() {
return this.rows, + ", " + this.columns;
}
this.copy = function() {
return new Matrix(this.rows, this.columns);
}
}
var t = new Matrix(6, 3);
There are more sophisticated ways to do this, using Object.create
. 使用Object.create
有更复杂的方法来执行此操作。 Look at Javascript: The Good Parts . 看一下Javascript:好的部分 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.