如何将对象的属性值传递给函数

时间:2020-03-06 22:50:59

标签: javascript

我是JavaScript的初学者,我正在尝试解决此任务: 有一个嵌套的员工对象,每个员工声明如下:

var employees = {
    employee_1: {
        name: NAME,
        workingHoursPerDay: NUMBER,
        paymentPerHour: calculatePaymentPerHour(workingHoursPerDay),
        salary: calculateSalary(workingHoursPerDay, paymentPerHour),
    },
}

所以我试图通过发送一个workingHoursPerDay值来声明为:

function calculatePaymentPerHour(workingHours)
{
    if (workingHours > 8)
        return 150
    else if (workingHours < 8)
    {
        if (workingHours >= 4)
            return 80
        else
            return 30
    }
    else
        return 100
}

,它将返回值以将其存储在paymentPerHour属性中 但出现错误:

未捕获的ReferenceError:未定义workingHoursPerDay

所以我尝试通过将其传递给函数后添加此属性来发送workingHoursPerDaycalculatePaymentPerHour(this.workingHoursPerDay), 但是我发现workingHoursPerDay的值以未定义的形式发送给函数。

那么如何将实际值发送给函数?

2 个答案:

答案 0 :(得分:0)

初始化对象时,您无法访问其属性。

这里有几种替代方法(可行)。

// Method 1 - Create object
var employees = {
    employee_1: {
        name: NAME,
        workingHoursPerDay: NUMBER,
        paymentPerHour: ,
        salary: calculateSalary(workingHoursPerDay, paymentPerHour),
    },
}
employees.employee_1.paymentPerHour = calculatePaymentPerHour(employees.employee_1)
employees.employee_1.salary = calculateSalary(employees.employee_1.workingHoursPerDay, employees.employee_1.paymentPerHour)

// Method 2 - Calculate first, then create Object
const paymentPerHour = calculatePaymentPerHour(NUMBER)
const salary = calculateSalary(NUMBER, paymentPerHour)
const employees = {
  employee_1: {
    name: NAME,
    workingHoursPerDay: NUMBER,
    paymentPerHour, // Shorter way of writing 'paymentPerHour: paymentPerHour,'
    salary, // See above
  }
}

答案 1 :(得分:0)

我认为您的问题与import serial import matplotlib.pyplot as plt import matplotlib.animation as animation arduinoData = serial.Serial('/dev/ttyACM0', 9600, timeout=1) arduinoArray = [] pr = [] # photoresistor ls = [] # led state # plot config fig = plt.figure() ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(2, 1, 2) # define a function used in animation function def plotting(i): # if no data coming in, then don't read data while arduinoData.inWaiting() == 0: pass arduinoString = arduinoData.readline().strip() arduinoArray = arduinoString.decode('ascii').split(',') f0 = float(arduinoArray[0]) f1 = float(arduinoArray[1]) # print(f0) # print(arduinoArray[0]) pr.append(f0) ls.append(f1) # plotting ax1.cla() ax1.plot(pr) ax2.cla() ax2.plot(ls) ani = animation.FuncAnimation(fig, plotting, interval=250) plt.show() 关键字的上下文有关。您的函数this是在calculatePaymentPerHour上下文中声明的,因此当您使用global调用它时,this.workingHoursPerDay关键字引用了全局对象this

您可以使用window方法来显式设置bind的值。

this

var employees = { employee_1: { name: NAME, workingHoursPerDay: NUMBER, paymentPerHour: calculatePaymentPerHour, salary: calculateSalary, }, } employees.employee_1.paymentPerHour.bind(employees.employee_1, employees.employee_1.workingHoursPerDay) 函数创建了bind的副本,该副本设置了提供的calculatePaymentPerHour上下文和要传递给该函数的参数。

this相同。

您可以查看有关calculateSalary上下文here

的更多信息