使用requirejs定义模块时为什么需要返回? [英] Why return is required while defining my module using requirejs?
问题描述
以下是我的代码:
HTML:
< !DOCTYPE html >
< span class =code-keyword>< html xmlns = http://www.w3.org/1999/xhtml >
< head >
< title > 使用Require js进行模块化加载脚本。< / title >
<! - Requirejs首先加载data-main中指定的模块。所以它将加载模块init,init有两个依赖项首先是knockout所以它加载了先淘汰,然后是myAppViewModel - >
< script type = text / javascript data-main = scripts / init.js src = 脚本/ require.js温泉n> > < / < span class =code-leadattribute> script >
< / head >
< 正文 >
名称:< ; span data-bind = text:myName > < / span >
< / body >
< / html >
init.js:
require([' knockout-3.2.0',' < span class =code-string> myAppViewModel'], function (ko,myAppViewModel){
ko.applyBindings( new myAppViewModel());
});
myAppViewModel.js
< pre lang =Javascript> define( function (){
return function myAppViewModel(){
this .myName = ' Sudheer';
};
});
如果我从上面的代码中删除了退货。它说myAppViewModel()不是构造函数。为什么在此上下文中需要返回?
它以 new myAppViewModel 部分开始...... new < JavaScript中的/ code>看起来像一个关键字,但实际上是运算符
!!!任何操作员也必须返回一个值!
实际上 new
实际上是调用你给他的函数作为参数 - 是的,这个是一个函数,即使你喜欢把它看作一个类(实际上在JavaScript中没有比语义更多的区别)...
在普通的JavaScript环境中你可以创建myAppViewModel而没有敲门的信封.js要求,如下所示:
function myAppViewModel(){
this .myName = ' Sudheer';
}
在这种情况下,new运算符能够在没有返回的情况下从函数中创建一个新类,但是在knockout.js中你附上了你的函数/在另一个函数/类中定义它(定义它被调用)你必须显式地将正确的答案返回给new ...
这就是为什么它在没有返回时就无法工作...
Hi,
Following is my code:
HTML:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Using Require js for modular loading of scripts.</title>
<!--Requirejs first loads the module that is specified in data-main .. So it will load the module init,init has two dependencies first is knockout so it loads the knockout first, then myAppViewModel next -->
<script type="text/javascript" data-main="scripts/init.js" src="scripts/require.js"></script>
</head>
<body>
Name : <span data-bind="text: myName"></span>
</body>
</html>
init.js:
require(['knockout-3.2.0', 'myAppViewModel'], function(ko, myAppViewModel) {
ko.applyBindings(new myAppViewModel());
});
myAppViewModel.js
define(function () {
return function myAppViewModel() {
this.myName = 'Sudheer';
};
});
If i remove return from above code. It says myAppViewModel() is not a constructor. Why return is required in this context?
It is beginning with new myAppViewModel part...new
in JavaScript is looks like a keyword but actually anoperator
!!! As any operator also new has to return a value!
What actuallynew
does is calling the function you gave him as parameter - and yes, this is a function even you like to think it as a class (in fact in JavaScript there is no difference more than semantics)...
In the normal JavaScript environment you would create myAppViewModel without the envelop that knockout.js requires, like this:
function myAppViewModel() { this.myName = 'Sudheer'; }
In this case new operator was able to create a new class out of the function without return too, but in knockout.js you enclose your function/class inside an other function/class (define it called) you have to explicitly return the correct answer to new...
That's the reason it will not work without return at the beginning...
这篇关于使用requirejs定义模块时为什么需要返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!