在NewsView.js里有这么一行代码
[code="java"]
NewsView.prototype.gridLand= function() {
var data1=[ [1, 'asdasd', 'asdasd'],
[2, 'bsvbcv', 'asdwq1'] ];
var store1=new Ext.data.SimpleStore({data:data1,fields:["id","name","mina"]});
var grid1 = new Ext.grid.GridPanel({
id : 'LandGrid',
trackMouseOver : true,
disableSelection : false,
autoScroll : true,
loadMask : true,
sortable : false,
height:150,
width:500,
columns:[{header:"sawayika",dataIndex:"name"},
{header:"yisilan",dataIndex:"mina"}],
store:store1,
});
return grid1;
};
[/code]
如果把NewsView看做一个类的话,那么gridLand就是这个类得一个方法 这样理解没错吧
我在另外一个js文件里是这么调用的
[code="java"]
var view=new NewsView();
alert(view.gridLand()); //alert一下看是不是object
[/code]
报错啊,没有gridLand这个方法
好吧换个方式
[code="java"]
new NewsView().gridLand();
new NewsView().gridLand;
NewsView().gridLand();
NewsView().gridLand;
NewsView.gridLand();
NewsView.gridLand;
gridLand;
gridLand();
[/code]
各种组合我都试完了,这tm到底是要闹那样啊
亲们,你说怎么拿到这个object?
你先试试把
return this.getView(); 这个先去掉,然后
NewsView.prototype.gridLand=function(){
alert("hello");
}
然后var test=new NewsView();test.gridLand()这样试试。
[quote]我在另外一个js文件里是这么调用的 [/quote]
那你包含NewsView的js应该放在上面,另外一个放下面
你的“NewsView”定义在哪里呢,贴出来看看。
因为你new NewsView()的时候其实是调用返回的 return this.getView(); 而getView方法返回的是new Ext.Panel对象,所以就不行了。
[quote]
When the [[Construct]] property for a Function object F is called, the following steps are taken:
1.Create a new native ECMAScript object.
2.Set the [[Class]] property of Result(1) to "Object".
3.Get the value of the prototype property of F.
4.If Result(3) is an object, set the [[Prototype]] property of Result(1) to Result(3).
4.If Result(3) is not an object, set the [[Prototype]] property of Result(1) to the original Object prototype object as described in 15.2.3.1.
6.Invoke the [[Call]] property of F, providing Result(1) as the this value and providing the argument list passed into [[Construct]] as the argument values.
7.If Type(Result(6)) is Object then return Result(6).
8.Return Result(1).
[/quote]
这段说明来自From the ECMA-262 3rd. Ediion Specification的[url=http://bclary.com/2004/11/07/#a-13.2.2]13.2.2[/url] [[Construct]]
这一段描述了当我们使用“new”操作时,javascript产生对象的过程,从第7步和第8步,可以看出,只要当地6步(就是调用构造函数本身)返回的不是一个Object类型时,才会执行到第8步,返回第一步构造的对象,否则直接返回构造函数执行后的结果。
所以,从你上面的例子来看,NewsView作为构筑函数,本身返回了一个Object类型的值,所以最终你通过“new”得到的是这个返回值,而不是你期望的。