javascript - 如何从作为类/对象一部分的外部函数中存在的内部函数获取数据?
问题描述
我正在学习如何使用 JavaScript 创建对象和类,我很抱歉在代码中创建了太多注释。这是为了帮助我记住重要的概念。我对 JavaScript 很陌生!我的问题是,我试图从类或对象外部访问函数,但如果没有语法错误,我似乎无法做到这一点。我假设是因为变量或方法超出了范围。那么如何调用类中存在的方法?我希望对象显示我创建的任何对象的周长。请帮忙!
class ball {
// get method used to access properties about the class/object
get ballInfo() { // constructor name should be about the objects behavor or algorithem.
this.radius = undefined;
this.diameter = undefined; // default values for undeclared values or strings should be kept as 'undefined'. For objects use 'null' instead.
this.pie = 3.14;
// defining variables that can be used out of scope for the functions below:
let displayDiameter = theDiameter(diameter);
let displayRadius = theRadius(radius);
// function1of2:
function theDiameter(diameter) {
let circumferenceTwo = pie*diameter;
return circumferenceTwo;
// function2of2
function theRadius(radius) {
let circumferenceOne = 2*pie*radius;
return circumferenceOne;
if (this.radius = undefined) {
/* NOTE: calling this method outside of this class or object
will give you the 'unexpected identifer error' because it is out of scope!
*/
console.log("The circumference for the ball is (msg1)" + (displayDiameter(6)));
// The fact that it's not outputting the console.log statement implies that the argument is not true for the if statement
}
if (this.diameter = undefined) {
console.log("The circumference for the ball is (msg2)" + (displayRadius(4)));
}
// Unfornately I can't seem to find a way to display the diameter on the screen.
console.log("displaying the circumference using the diameter.... " + displayDiameter(6));
} // end of theRadius function
} // end of theDiameter function
} // end of ballInfo function
}; // end of class
解决方案
不确定您的类的逻辑,但这是您的代码的工作版本,具有正确的语法和函数,可以访问对象的内部上下文,并且可以从对象外部访问:
class Ball {
constructor(diameter) {
this.radius = diameter / 2;
this.diameter = diameter;
this.pie = 3.14;
}
theDiameter(diameter) {
let circumferenceTwo = this.pie * this.diameter;
return circumferenceTwo;
}
theRadius(radius) {
let circumferenceOne = 2 * this.pie * this.radius;
return circumferenceOne;
if (this.radius = undefined) {
console.log("The circumference for the ball is (msg1)" + (displayDiameter(6)));
// The fact that it's not outputting the console.log statement implies that the argument is not true for the if statement
}
if (this.diameter = undefined) {
console.log("The circumference for the ball is (msg2)" + (displayRadius(4)));
}
// Unfornately I can't seem to find a way to display the diameter on the screen.
console.log("displaying the circumference using the diameter.... " + displayDiameter(6));
} // end of theRadius function
}; // end of class
let ball = new Ball(10);
console.log(ball.theDiameter());
console.log(ball.theRadius());
只需单击Run code snippet即可查看它的实际效果
推荐阅读
- c - 为什么我的递归函数即使到达返回(0)也不会停止?
- python - 在 python 中通过循环从一个文本文件到另一个文本文件进行搜索,同时每个搜索项仅返回一个结果
- corda - 在 Windows 上运行 Cordite 网络映射 JAR 时无法运行程序 npm 错误
- c# - 在http-context响应C#中分块下载文件
- mongodb - 从 getMongo 处理器中提取日期时的 $numberlong 格式
- javascript - setTimeout 如何在 JavaScript 中实现 promise?
- angularjs - AngularJS 延迟加载路由无法正常工作
- android - 验证 RecyclerView 中的重复条目
- php - 当我上传带有变体的更新产品的 excel 时,shopify Api 中出现错误(api 客户端每秒超过 2 次调用)
- git - 无法在 AWS CodeCommit 中合并拉取请求。抱怨有冲突,但是远程和本地都是最新的