js基础的问题请教?

js初初级 2014-01-06 11:51:19

window.onload = function() {

function Base() {
this.elements = [];
}

Base.prototype.getId = function(id) {
this.elements.push(document.getElementById('a'));
alert(this); //这行的this
};
Base.getId('a');
};


错误提示是:TypeError: Base.getId is not a function
为什么啊?
请问this指的是Base对象还是Base的原型对象Base.prototype呢?
谢谢!
...全文
92 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
孤飞不倦 2014-01-07
  • 打赏
  • 举报
回复
楼主不够细心啊~~对象根本没有创建啊。 至于this,是永远指向当前运行的环境的。
function test() {
    console.log(this.x);
}
var o = {};
o.x = 1;
o.m = test;
o.m();
test();
运行一下就大概理解了。
w51422 2014-01-07
  • 打赏
  • 举报
回复
Base本身是个对象 可以用for(var i in Base){ alert(Base[i]+" "+i); } 或者JSON.stringify(Base) 查看Base这个对象的属性 new Base();产生新的对象 获得Base.prototype原型链上的属性
似梦飞花 2014-01-07
  • 打赏
  • 举报
回复
base的对象
bobbyworm 2014-01-07
  • 打赏
  • 举报
回复

window.onload = function() {
 
  function Base() {
    this.elements = [];
  }
 
  Base.prototype.getId = function(id) {
    this.elements.push(document.getElementById('a'));
    alert(this);  //这行的this
  };

  var base = new Base();
  base.getId('a');
};
要new!

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧