首页 \ 问答 \ Javascript中基本原型的问题(Problems with basic prototyping in Javascript)

Javascript中基本原型的问题(Problems with basic prototyping in Javascript)

我有点新来的JavaScript和我开始的东西,我坚持一些基础知识,事情是我想创建一个对象的原型,然后创建对象的数组引用,然后accesing他们的方法,但我在哪里有人可以帮助我解决这个问题,我在做什么显示在这里: -

function Obj(n){
    var name=n;
}
Obj.prototype.disp = function(){
    alert(this.name);
};
var l1=new Obj("one");
var l2=new Obj("two");
var lessons=[l1,l2];
//lessons[0].disp();
//alert(lessons[0].name);

但这些方法似乎都没有解决.... :(


i am kinda new to javascript and i started off on something where i am stuck with some basics, the thing is i am trying to create an prototype for an object and then the references of created objects in an array and then accesing their methods but i am wrong somewhere can anyone help me with this, what i am doing is shown here :-

function Obj(n){
    var name=n;
}
Obj.prototype.disp = function(){
    alert(this.name);
};
var l1=new Obj("one");
var l2=new Obj("two");
var lessons=[l1,l2];
//lessons[0].disp();
//alert(lessons[0].name);

but none of these methods seem to work out.... :(


原文:https://stackoverflow.com/questions/17925901
更新时间:2022-09-02 09:09

最满意答案

在我看来,你可以使用自定义视图来包装项目view.And在自定义视图中,你应该overMeasure方法:

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        // set your width and height 
        setMeasuredDimension(width, height);
}

另一个想法

当你创建layoutManager

manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {  
            @Override  
            public int getSpanSize(int position) {  
               if( position % 4 == 1 || position %4 ==2)
                   return 1;
               else
                   return 2;
            }  

  });  

经理将自动设置每个项目的宽度和高度,因此您不需要设置manualy


After almost a week of struggle, I found the fix. It's very stupid that I wasted 1 week on something like this, but here it is. Instead of calling the function setSpanSizeLookup() in the OnCreateViewHolder in CustomAdapter, it should be called when the adapter is called i.e.

GridLayoutManager lm = new GridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false);
lm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        int mod = position % 4;
        if(mod == 1 || mod == 2)
            return 1;
        else
            return 2;
    }
});
mCustomView.setLayoutManager(lm);
mCustomAdapter = new CustomAdapter(imagesList);
mCustomView.setAdapter(mCustomAdapter);

This fixed the bug for some unknown reason.

NOTE: Even though, Cole suggested almost the same thing, but it was not in context with the question. He could have answered my question correctly, if he had read my current code first.

相关问答

更多
  • 您应该使用flex-basis来指定柔性物品的初始长度。 body, html { height: 100%; } body { margin: 0; padding: 0; display: flex; flex-direction: column; } body>div { display: flex; } body>div>div { border: solid 1px blue; } div { flex-grow: 1 ...
  • 请注意,必须有一个整数个正方形来填充宽度和高度。 因此,纵横比必须是有理数。 输入: width (float或int), height (float或int) 算法: aspectRatio = RationalNumber(width/height).lowestTerms #must be rational number # it must be the case that our return values # numHorizontalSqaures/numVerticalSquares = ...
  • 所以我看到你的代码有两个问题 您最后两个项目的列号是相同的,这就是它堆叠的原因。 您没有为每个视图提供正确的layout_gravity 。 这是您工作的修改后的代码
  • 我找到了解决方案。 最后我将GridLayout设置为3行1列。 及其宽度和父宽度为match_parent。 每行上的LinearLayout及其子项的宽度设置为wrap_content。 ImageViews本身就是那些LinearLayouts的子代,解决方案的关键部分是它们的layout_weight等于“0.3”。 不需要引力或其他任何东西。 layout_weight =“0.3”,ImageViews的屏幕大小为1/3,GridLayout的父元素宽度设置为match_parent。 I fo ...
  • 由于浮动的性质,您想要实现的目标不适用于浮动容器。 float:left; 表示容器尽可能向左移动,或者如果没有足够的水平空间,则向下移动到必要的位置,然后向左移动。 在你的情况下,中间有两个巨大的正方形。 在这些之后,第一个方块放在它们的右边,但是第二个方块需要向下移动,因为它缺少水平空间。 降档后,它仍然无法向左移动,因为中间仍然有一个巨大的正方形...... 要实现您想要的,您可以使用带有colspan和rowspan html table ,或者您可以使用容器的绝对定位,具体取决于您的要求。 Wha ...
  • 在我看来,你可以使用自定义视图来包装项目view.And在自定义视图中,你应该overMeasure方法: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // set your width and height setMeasuredDimension(width, height); } 另一个想法 当你创建layoutManager : manager ...
  • 当使用BoxLayout时,您必须为其中的任何元素设置对齐方式。 如下例所示: http : //docs.oracle.com/javase/tutorial/uiswing/layout/box.html 修复对齐问题 你必须为你的实验室和你的网格设定对齐方式 lab.setHorizontalAlignment(SwingConstants.LEFT); yourGrid.setAlignmentX(Component.LEFT_ALIGNMENT); When using BoxLayout yo ...
  • 正如@Morfic所提出的那样,我在自动模式下启用了Server-Push插件,现在客户端所需的更新由该插件完成。 不知道我有多喜欢这个解决方案,但它工作。 As proposed by @Morfic I enabled the Server-Push addon in automatic mode and now the required updates to the client are made by that addon. Not sure how much I like that solutio ...
  • 这个问题有两个来源: 您的文件编码不正确。 如果您使用的是ASCII字符以外的任何内容,则需要有意识地决定选择正确的编码。 复制和粘贴工作的事实使我认为你的编码是正常的,但你仍然没有意识到这是在幕后发生的事实。 您的编辑器正在尝试使用无法正确呈现它们的字体来显示unicode字符。 并非每种字体都支持Unicode。 将字体更改为可以呈现Unicode字符的字体,您应该很高兴。 编辑:作为旁注,理解这里的笑话也会对你有所帮助。 There are two sources to this problem: Y ...
  • 提供的代码创建了一个kd树 。 您可以使用它在矩形上绘制线条,将其划分为更小的矩形。 获得树后,您可以按如下方式使用它来将您的区域划分为这些矩形: 选择树根处的节点。 在此点绘制一条垂直线。 选择它的左子,在刚刚通过它的父线绘制的线左侧的这一点画一条水平线(这条线在你刚绘制的线上停止)。 选择它是正确的孩子,通过它刚刚通过它的父线绘制的线右侧的这一点绘制一条水平线(此线也在您通过父线绘制的线上停止)。 递归地执行此操作,在树的每个级别切换垂直和水平线。 码: int MAX_HEIGHT = 100; in ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。