帮助原型对象

时间:2010-04-03 12:48:31

标签: javascript prototype

我正在学习javascript并且在通过原型创建一个onject时遇到一些麻烦 我有这个:

 <script type="text/javascript">

        function myclass(a, b, c) {
            if (arguments.length) { this.Init(a, b, c); }
        }
        myclass.prototype.Init = function(a, b, c) {
            this.param1 = a;
            this.param2 = b;
            this.param3 = c;
        };
        myclass.prototype.Print = function() {

            alert(this.param1 + '-' + this.param2 + '-' + this.param3);
        };

        var myObject = myclass(3, 5, 6);
        myObject.Print();


    </script>

但是我得到了一个与此相符的错误.Init(a,b,c);
错误:对象不支持此属性或方法

2 个答案:

答案 0 :(得分:3)

当您声明new

时,您忘记了myObject关键字
var myObject = new myclass(3, 5, 6);

答案 1 :(得分:0)

出于好奇,有一个特殊的原因你有一个单独的“init”方法吗?

定义“类”的函数称为“构造函数”,您可以在那里执行设置。如果你想“重新初始化”这个对象,那么它可能会有所帮助,但它似乎没有在这里提供一点。

例如:

// You might as well start wrapping your code now:

var myExample = (function myExample () {

    // A common convention is to start the name of constructors with a
    //      capital letter, one reason is it help makes it more obvious
    //      when you forget the new keyword...Whether you use it or not
    //      is up to you. Also note, calling it "MyClass" is a little
    //      misleading because it's not a "class" really. You might
    //      confuse yourself if you think of it as a class too much.
    //      If you're wondering why I put the name twice, it's because
    //      otherwise it would be an anonymous function which can be
    //      annoying when debugging. You can just use var MyClass = function () {}
    //      if you want

    var MyClass = function MyClass(a, b, c) {

        // This will set each parameter to whatever was provided
        //      or if nothing is provided: null. If you leave out
        //      the || "" part then any
        //      time a value is not provided the parameter will
        //      return "undefined". This may be what you want in some cases.

        this.param1 = a || "";
        this.param2 = b || "";
        this.param3 = c || "";   
    };

    // likewise it's convention to start most variables/functions lowercase
    //      I think it's easier to type/looks better, but do as you please.

    MyClass.prototype.print = function print() {
        alert(this.param1 + '-' + this.param2 + '-' + this.param3);
    };

    var myObject = new MyClass();
    myObject.print();
}());

“包装”是

(function () {
 //your code here 
}());

这里大部分都没有意义,但这是你最终要开始做的事情,所以不妨从现在开始。这只是“包装”的一种方式,也有其他方式。

基本上,编写脚本的方式,如果用户运行另一个具有名为MyClass的函数的脚本,它可能会覆盖您的脚本,反之亦然,从而导致问题。

“包装”将其全部保留在该功能中。如果你需要为外面的东西提供一些东西,你可以暴露它。

每条评论:

你可以通过将它们暴露在外面来访问包装器内部的函数和变量,如下所示:

var myApp = (function myApp(){

    // The constructor for our "class", this will be available from outside because
    //    we will expose it later

    var myClass = function(){
        //code to set up "class" etc


        // See how we can use private function within myApp
        privateFunction();
     };


    // Here we set up the private function, it will not be available outside myApp
    //    because will will not expose it
    var privateFunction = function(){ };


    // Another public function that we will expose later
    var otherPublic = function(){};

    //now we expose the stuff we want public by returning an object containing
    //    whatever it is we want public, in this case it's just myClass and otherPublic

    return { myClass: myClass, otherPublic: otherPublic  };
}()); 

请注意,在该示例中,如果您想要对象的实例,我们只是公开构造函数 您必须在变量中收集它们并公开该变量,如:

var theInstance = new myClass();
return { theInstance : theInstance };

现在可以在myApp外部以myApp.theInstance

的形式使用

您还可以使用更基本的包装方案:

var myApp =  {

    myClass: function(){

        //if we want to call another function in myApp we have to do it like so:
        myApp.publicFunction();
    },

    publicFunction: function(){},

    someString: "this is a string"

};

myApp只是一个包含你的函数等的对象文字。主要区别在于myApp中的所有内容都可以通过myApp.name或myApp [name]从外部访问;