我有一个var:
var main= {
init: function (model) { // model is an object
test.init();
main.model = model;
}
}
var test = {
init: function () {
**Solved** my problem is when i loged it, what i actualy did was:
console.log('result:' + main.model);
//and it failed to concrate string with object
}
}
' main.model'找不到。
如何从' main'中访问对象?在'测试'?
修改
调用main.init()的实际代码在cshtml中:
$(document).ready(function () {
var model = @Html.Raw(Json.Encode(Model))
main.init(model);
});
答案 0 :(得分:2)
main.model
实际上是未定义的。在您实际调用main.model
函数之前,main.init()
对象不存在。
var main = {
init: function () {
main.model = 'model';
}
}
var test = {
init: function () {
console.log(main.model);
}
}
main.init()
test.init()
编辑:同样的故事。
var main= {
init: function (model) { // model is an object
main.model = model;
}
}
var test = {
init: function () {
console.log(main.model.foo); //<--- I get [object Object]
}
}
main.init({foo:'bar'})
test.init()