对象的值未定义

时间:2017-02-02 18:29:37

标签: javascript object undefined iife

我正在学习JavaScript。我已经尝试了下面的代码,我发现一个对象的值是未定义的。我希望最后一个控制台语句打印对象v中的所有值。我的期望是这个语句v.sampleFunction()将返回“给定的字符串是样本字符串样本字符串”。我认为我可以使用 v.values 打印相同的内容。请告诉我我在做错的地方。

var r;
var v = new Object();

(function() {
    // function 1
    function s(sampelString) {
        var t = "The given String is "+sampelString;
	return (t);
    }
    r = s("Sample Text");
    
    // Adding SampleFunction to v
    v.samepleFuntion = function() {
        s("Sample String Sample String");
    }
})();
v.samepleFuntion();
console.log("The values in r==="+r);
console.log("The values in v==="+v.values);

1 个答案:

答案 0 :(得分:5)

您需要返回函数调用的值

Public Function AverageCell(allItems As String) As Variant

Dim itemArray() As String
Dim totalsum As Variant
Dim totalav As Long

totalsum = 0
totalav = 0

itemArray() = Split(allItems, ",")

    For Each num In itemArray()
        totalsum = totalsum + num
        totalav = totalav + 1
    Next num

AverageCell = totalsum / totalav

End Function



return s("Sample String Sample String");
//^^^^