开发者

Why do I never see the prototype property in JavaScript code I edit?

开发者 https://www.devze.com 2023-01-02 17:39 出处:网络
Why do I never see the prototype property in JavaScript code I edit, from books and documentation I\'ve read, it seems to be a cornerstone of the l开发者_如何学运维anguage.Maybe because the majority o

Why do I never see the prototype property in JavaScript code I edit, from books and documentation I've read, it seems to be a cornerstone of the l开发者_如何学运维anguage.


Maybe because the majority of all javascript coders never cared to learn the basics of the language, and because loose approach allows for a lot of different ways of solving things.


in the work i've done, prototype usually gets abandoned in favor of closure methods defined within the constructor so I can have private members in my javascript class. i'm aware that prototype may serve a purpose when setting up inherited classes but i've never needed to approach that level of complexity.


The prototype property only exists on Function objects. Other objects do not have a prototype property. It refers to the object that is used as the prototype for any object created by that function when used as a constructor.

function Thing() {
}

Thing.prototype = {
    foo: "bar"
};

var t = new Thing();
window.alert(t.foo);


I don't know if an example is a solution, but this is an example of using a prototype.

Group.prototype['somethin'] must be defined, but Group.prototype exists when you create a new Group.

function Group(ob){
    if(!ob || typeof ob!= 'object') ob= {};
    for(var p in ob){
        if(ob.hasOwnProperty(p)) this[p]= ob[p];
    }
}
Group.prototype.merge= function(ob, force){
    var tem, p, force= force!== false;
    if(ob && ob.hasOwnProperty){
        for(p in ob){
            if(ob.hasOwnProperty(p)){
                tem= this[p];
                if(tem=== undefined || force) this[p]= ob[p];
            }
        }
    }
    return this;
}
Group.prototype.assignTo= function(ob, ob2, force){
    return this.merge.call(ob, ob2, force);
}


What IDE are you using? Try visual web developer express

0

精彩评论

暂无评论...
验证码 换一张
取 消