创建单个对象

发布于 2024-11-29 10:47:41 字数 169 浏览 2 评论 0原文

我想创建一个对象。下面的代码有意义吗?

singleObj = new function () {
    // act as a constructor.
};

我是否伤害了任何好的做法?

我需要一个构造函数。简单的对象文字在这里没有用。

I want to create a single object. Does the below code make sense?

singleObj = new function () {
    // act as a constructor.
};

Am I hurting any good practice?

I need a constructor. A simple object literal would not be useful here.

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

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

发布评论

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

评论(4

同展鸳鸯锦 2024-12-06 10:47:41

如果您只想要一个对象,因为您永远不会再创建一个对象,那么对象文字在这里就可以完美地工作。

var x = { };

会给你一个对象。
为了

var F = function() {
};

给你一个对象,你需要调用F

var x = new F();

If you want just a single object, in that you are never going to make one again, an object literal works perfectly here.

var x = { };

Will give you an object.
In order for

var F = function() {
};

to give you an object you will need to invoke F

var x = new F();
尐籹人 2024-12-06 10:47:41

你可以尝试这样的事情:

var objCreate = function() {
    var obj = {};
    // do constructor logic
    return obj;
};

you could try someting like:

var objCreate = function() {
    var obj = {};
    // do constructor logic
    return obj;
};
壹場煙雨 2024-12-06 10:47:41

只需创建一个新对象然后填充它即可。您不需要委托人来执行此操作。

var singleObject = {};
singleObject.value1 = "whatever";

如果你真的想使用一个函数,那么你需要实际调用它。

var singleObj = new (function () {
    // act as a constructor.
})();

我们可以通过创建一个匿名函数 function(){} 来使用自执行函数,并立即使用空参数集调用它。

Just create a new object and then populate it. You don't need a contrustor to do this.

var singleObject = {};
singleObject.value1 = "whatever";

If you really want to use a function, then you need to actually call it.

var singleObj = new (function () {
    // act as a constructor.
})();

We can use a self executing function by creating a anonymous function function(){}, and immediately calling it with an empty argument set.

不再见 2024-12-06 10:47:41

http://www.w3schools.com/js/js_objects.asp

//Used to create an object
personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";

//Used as a constructor for the object
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
//how to declare objects via constructor template
var myFather=new person("John","Doe",50,"blue");
var myMother=new person("Sally","Rally",48,"green");

http://www.w3schools.com/js/js_objects.asp

//Used to create an object
personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";

//Used as a constructor for the object
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
//how to declare objects via constructor template
var myFather=new person("John","Doe",50,"blue");
var myMother=new person("Sally","Rally",48,"green");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文