简体   繁体   English

此javascript代码块是什么意思?

[英]What does this javascript code block means?

I came accross a code block in javascript. 我遇到了JavaScript中的一个代码块。 Can you please tell me its meaning? 你能告诉我它的意思吗?

var result = {
    diagmetric : diag * 2.54,
    sizex : xd,
    sizey : yd,
    metricsizex : 2.54*xd,
    metricsizey : 2.54*yd,
    xppi : x/xd,
    yppi : y/yd,
    dotpitch : pitch,
    sqppi : x/xd*y/yd
};

Full function : 功能齐全

function calc_dpi (x,y,diag) {
    if (y == 0 || x == 0) return;
    var ratio = y/x;
    var xd = Math.sqrt( Math.pow(diag,2) / ( 1 + Math.pow(ratio, 2) ));
    var yd = xd * ratio;
    var pitch = 25.4/(x/xd); // metric
    var result = {
        diagmetric : diag * 2.54,
        sizex : xd,
        sizey : yd,
        metricsizex : 2.54*xd,
        metricsizey : 2.54*yd,
        xppi : x/xd,
        yppi : y/yd,
        dotpitch : pitch,
        sqppi : x/xd*y/yd
    };
    return result;
}

Here you asign the Object to the result var. 在这里,您将对象分配给result var。 In JS you can define the fields in the object this way: 在JS中,您可以通过以下方式在对象中定义字段:

var obj = {fieldA:A, fieldB:B};

That's a JS Object Literal. 那是一个JS对象文字。 Think of it as an associative array. 将其视为关联数组。

https://stackoverflow.com/a/3831209/1410212 https://stackoverflow.com/a/3831209/1410212

Syntactically, it's just a literal object assignment. 从句法上讲,这只是一个文字对象分配。 A new object is created by the {...} expression with the specified properties and values. {...}表达式使用指定的属性和值创建一个新对象。 Semantically, it packs the return values of the function in a new plain object and returns the object. 语义上,它将函数的返回值打包到一个新的普通对象中并返回该对象。

it defines a javascript object using 6 parameters (that are surely defined as function parameters into which the var is defined): 它使用6个参数(肯定是定义为var的函数参数)定义了一个javascript对象:

x,y,diag,xd,yd,pitch X,Y,诊断,XD,YD,音高

i suppose xd & yd are inches size x & y are pixel size diag : i suppose diagonal pitch : some pitch info. 我想xd和yd是英寸大小,x&y是像素大小诊断:我想对角线间距:一些间距信息。

this javascript object stores structured info (as any object) that define 此javascript对象存储定义了结构化信息(作为任何对象)

This function calculates the dpi and returns an object with the results. 此函数计算dpi并返回带有结果的对象。 The Object is written in literal notation. 该对象以文字符号编写。 It's a common way in javascript to not only return a single value from a function but a full object or even another function. 在JavaScript中,不仅从函数返回单个值,而且从完整对象甚至另一个函数返回值都是一种常见的方式。

You can use it the following way: 您可以通过以下方式使用它:

var result = calc_dpi(/* your parameters*/)
// now you have access to the result by calling the properties
// of the result object e.g. with:
alert(result.dotpitch);

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

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