简体   繁体   English

如何在另一个对象内分配对象

[英]How to assign object inside another object

Below is the function that does some stuff. 下面是执行某些操作的函数。

ns.Line2Point = function (params) {
        points = params.points;
        // code here
};

In the code above, params object contains a points object . 在上面的代码中, params object包含一个points object So, when I want to call Line2Point method, I must create a object, and it contain points object. 因此,当我想调用Line2Point方法时,必须创建一个对象,它包含点对象。 But, I don't know how to in Javascript. 但是,我不知道如何使用Javascript。 I just have some ideas like the code below, but don't know it true or false: 我只是有一些想法,例如下面的代码,但不知道它是对还是错:

    var params;
    var points;
    // some code to initialize for points object
    params.points = points
    ns.Line2Point(params)

Please teach me this. 请教我这个。 Thanks :) 谢谢 :)

as you said you just need to initialize params. 如您所说,您只需要初始化参数。

var params = {};   // initialize to an object literal
params.points = {}; // points is now an object literal, too

if you already have points , initialize params , then do 如果您已经有了points ,请初始化params ,然后执行

params.points = points; // assign points to the points property of params

I don't really understand your question, but I think this is what you're looking for: 我不太了解您的问题,但是我认为这是您想要的:

points = [
    {
        x: 10,
        y: 25
    },
    {
        x: 20,
        y: 35
    }
]

Points is an array now, that consists of 2 objects, that consist of 2 variables each: x and y . 点现在是一个数组,由2个对象组成,每个对象由2个变量组成: xy

Declare params as object first. 首先将参数声明为对象。

var params = {}; var params = {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM