我正在从头开始学习,并试图了解如何更好地从函数中获取值.请考虑以下示例.
/* Screen Orientation Check */
function screenOrientation () {
var screenOrientationCheck = ($(window).width() > $(window).height())? 90 : 0;
console.log(screenOrientationCheck) ;
}
screenOrientation();
Run Code Online (Sandbox Code Playgroud)
上面给我屏幕方向.
/* Viewport Height Check */
function viewportHeight () {
var viewportHeightCheck = document.documentElement.clientHeight
console.log('viewportHeight = '+viewportHeightCheck);
};
viewportHeight();
Run Code Online (Sandbox Code Playgroud)
这会给我视口高度.
但是现在,如果我喜欢使用这些功能的结果,我不知道该怎么做.
if ( viewportHeight() > 600 ) {
console.log('Hooray!!');
};
Run Code Online (Sandbox Code Playgroud)
例如,这永远不会发生.
if ( screenOrientation() === 90 ) {
console.log('Hooray!!');
};
Run Code Online (Sandbox Code Playgroud)
同样,这也永远不会发生.
如果我想记录screenOrientationCheck或者viewportHeightCheck是undefined因为变量只在函数的范围存在.我已经明白了.添加return到下面的任何一个函数也不起作用.
/* Viewport Height Check */
function viewportHeight () {
var viewportHeightCheck = document.documentElement.clientHeight
console.log('viewportHeight = '+viewportHeightCheck);
return viewportHeightCheck;
};
Run Code Online (Sandbox Code Playgroud)
我知道这是基本的东西.因此,我很遗憾花费您宝贵的时间来问这个问题.
我试图理解如何使用函数中创建的值并在代码中反复调用它们,而不是在运行时执行的匿名函数只能执行一次.
如果那里的人有勇气尽可能详细地回答这个问题,可能Hooray!!会有一个最终会记录的例子,那将是一个好的结局.
您需要返回该值.这是一个有效的Jsfiddle
function getViewportHeight() {
var viewportHeightCheck = document.documentElement.clientHeight
return viewportHeightCheck;
};
if ( getViewportHeight () > 600 ) {
console.log('Hooray!!');
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49 次 |
| 最近记录: |