Lexical declaration problems with default params [duplicate]

☆樱花仙子☆ 提交于 2020-01-30 10:54:48

问题


I'm having some problems when defining some functions after declaring a class.

I've used default params before when declaring functions, but I don't know if I can use a function or class as default parameter too.

My code is this

const Matrix = class {/*...some code...*/}

const sigmoid = function(A, flag = false, factor = 1, Matrix = Matrix) {
    /*my functions declaration*/
}

Here I have the problem.

var result1 = sigmoid( Matrix.dot( [[val1, val2]], res.W1 ) , false, 1)
var result2 = sigmoid( Matrix.dot(result1, res.W2), false, 1)

In the line of const sigmoid = ... it said can't access lexical declaration `Matrix' before initialization


回答1:


You're shadowing your Matrix identifier in the declaration by doing this:

const sigmoid = function(A, flag = false, factor = 1, Matrix = Matrix) {
// ---------------------------------------------------^

That means the Matrix after the = is the parameter, not the class identifier.

Just use a standard lower-case parameter name instead:

const sigmoid = function(A, flag = false, factor = 1, matrix = Matrix) {
// ---------------------------------------------------^


来源:https://stackoverflow.com/questions/48488873/lexical-declaration-problems-with-default-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!