繁体   English   中英

将Javascript“新对象”转换为文字符号

[英]Convert Javascript 'new object' to literal notation

我正在使用Aptana Studio 3编写Javascript OOP,我正在使用“新”和“此”创建类和对象,但是确实很烦人,因为没有IDE自动完成功能无法识别我的对象属性和方法。

也许如果我更改为文字表示法,则IDE会识别我的对象并启用“自动完成”功能,但是我无法将我的代码转换为保持相同功能的文字表示法。

我写了一个样本,通常是我写的。

var ClassPerson=function(name,lastName){

    //initialize stuff here
    alert(name + ' was just created now!');

    var time=setInterval( function(){getOlder(1);} ,(1000*60*60*24*365));

    //public properties
    this.name=name;
    this.lastName=lastName;

    //private properties
    var age=0;
    var weight=3;

    var parent=this; //reference to 'this' of this object, not the caller object.

    //public methods
    this.speak=function(text){  
        alert(text);
    }

    this.walk=function(steps){
        weight=weight-(0.05*steps);
    }

    this.eat=function(kcal){
        weight=weight+(kcal/2);
    }

    //private methods
    function getOlder(years){
        age=age+years;
    }
}

var me = new ClassPerson('Justin','Victor');
me.speak( me.name );
me.eat(2500);

如果有人可以将其转换为文字,也许我可以弄清楚它是如何工作的。

这是一个小例子:

var sample = function (a,b) {
  this.a = a;
  this.b = b;
} 
var sampleObj = new sample(0,1);
sampleObj.a == 0; // true    

var a = 0, b = 1;
var sample = {
    a: a,
    b: b
}
sample.a == 0; // true

//在不使用new关键字的情况下调用var johnsmith = ClassPerson('John','Smith')是否有帮助?

function ClassPerson(name, lastName){
    if(!(this instanceOf ClassPerson)) return new Classperson(name, lastName);
    //constructor code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM