[英]JavaScript objects and literal notation
I just cleared my mind on JavaScript objects and my question is really simple for most of the people here. 我只是对JavaScript对象清除了想法,对于这里的大多数人来说,我的问题确实很简单。 I feel comfortable with the JavaScript object literal notation like: 我对JavaScript对象文字表示法感到满意,例如:
var Obj = {
/**
* @type {string}
*/
name: '',
/**
* setName
* Set the `name` property.
*
* @param param
*/
set setName (param) {
this.name = param;
}
};
The only limit I found is that if I want to create two completely separate objects in the same page, with this notation I can't. 我发现的唯一限制是,如果我想在同一页面中创建两个完全独立的对象,则无法使用此符号。
var a = Obj;
a.setName = "John";
var b = Obj;
b.setName = "Peter";
// OUTPUT
// a.name -> Peter
// b.name -> Peter
Set var whatever = Obj
is just useless, 'cause it doesn't instantiate n
separate objects and it just overwrites the code above. 设置var whatever = Obj
都是无用的,因为它不会实例化n
单独的对象,并且只会覆盖上面的代码。 Using new
keyword such as var a = new Obj
doesn't work neither (probably I'm using it in the wrong way?). 使用诸如var a = new Obj
类的new
关键字也不起作用(可能是我以错误的方式使用了它?)。 The solution I came up with is returning the object inside a function, like: 我想出的解决方案是在函数内返回对象,例如:
function Obj() {
return {
/**
* @type {string}
*/
name: '',
/**
* setName
* Set the `name` property.
*
* @param param
*/
set setName (param) {
this.name = param;
}
}
}
This way I can create two different objects and correctly access to their properties and methods: 这样,我可以创建两个不同的对象并正确访问它们的属性和方法:
var a = Obj();
a.setName = "John";
var b = Obj();
b.setName = "Peter";
// OUTPUT
// a.name -> John
// b.name -> Peter
So, my question are: 所以,我的问题是:
Your concept of a function that returns an Object
instance is valid, but your implementation is very brittle because it is only set up to work with specific properties. 您返回一个Object
实例的函数的概念是有效的,但是您的实现非常脆弱,因为它只能设置为与特定属性一起使用。 Read on for more details on various ways to create instances and a more flexible way to return objects... 请继续阅读以获取有关创建实例的各种方式以及返回对象的更灵活方式的更多详细信息。
var a = Obj;
doesn't create a new object. 不会创建新对象。 It just assigns a
the memory address of the existing object Obj
. 它只是为现有对象Obj
分配a
内存地址。
var myObj = {}; // Object instance is created and memory location is stored in myObj var a = myObj; // No new object is created. a and myObj point to the same object console.log("Are 'a' and 'myObj' both pointing to the same object?", a === myObj); // true
If you want to design a single object and then make more of that object, you need to be able to create "instances" of an object. 如果要设计单个对象,然后再使用该对象,则需要能够创建对象的“实例”。 You can't do that directly with an object literal: 您不能直接使用对象文字来做到这一点:
var myObj = { someProp:10 }; var myNewObj = new myObj(); // Fails because an object literal can't be instantiated
But, you can do it with the Object.create()
method, which takes your Obj
concept to fruition : 但是,您可以使用Object.create()
方法来实现 , 该方法将Obj
概念变为现实 :
// Object instance is created and memory location is stored in myObj var myObj = { someProp: "default", // "Methods" are just properties with functions as their value someMethod: function(input){ // The || syntax that follows allows for a default value for the method // if no argument is passed to the method. this.name = input || "default"; } }; // Create a new Object instance and set myObj as the prototype of the instance. // This means that the new instance will inherit from that prototype: var a = Object.create(myObj); console.log(a.someProp); // "default"; a.someProp = "something specific"; a.someMethod("Test"); myObj.someMethod(); console.log(a.name, myObj.name); // "Test" "default" console.log(a.someProp, myObj.someProp); // "something specific", "default"
Instances can be explicitly made with the new
operator and a constructor function: 可以使用new
运算符和构造函数显式创建实例:
function foo(){ this.someProp = "something"; } var a = new foo(); // Unique instance of foo var b = new foo(); // Separate and distinct instance of foo a.someProp = 10; b.someProp = 20; console.log(a.someProp, b.someProp); // 10 20
Or, the new
operator and a class : 或者, new
运算符和一个类 :
class foo{ constructor(val) { this.someProp = val; } } var a = new foo(10); // Unique instance of foo var b = new foo(20); // Separate and distinct instance of foo console.log(a.someProp, b.someProp); // 10 20
Have you tried with Object.create() ? 您是否尝试过Object.create() ?
var b = { setName : "Mongo" }; a = Object.create(b); a.setName = "John"; b.setName = "Peter"; console.log(a.setName); console.log(b.setName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.