doudan4834 2012-02-16 22:08
浏览 39

将原型函数用于对象文字数组

I am using PHP, MySQL, and javascript. I use php to connect to my database to select appointments. I then echo them in a script tag as arrays of object literals (JSON objects):

appointment[$apptid] = {"time":"8:00", "date":"2012-02-10", "description":"testAppt"};
...

I chose to do it this way over writing an appointment "class" in case I add or remove appointment fields, however I can't figure out for the life of me how to create functions that will apply to this array of objects. Is there anyway to declare these as appointment objects and then write prototype functions without losing the properties?

  • 写回答

2条回答 默认 最新

  • duan32342 2012-02-16 22:17
    关注

    I'm not 100% sure that I understand what you're after, but if I understand then maybe this is what you want:

    First of all, in javascript you better use arrays in a different way than in php, so do something like:

    var appointments = [];
    appointments.push({"time":"8:00", "date":"2012-02-10", "description":"testAppt"});
    

    Now, once you the array you can do something like:

    function doSomething() {
        alert(this.time);
    }
    
    for (var i = 0; i < appointments.length; i++) {
        appointments[i].doSomething = doSomething;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计