我是mlearning javascript,在通过prototype创建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>但是我在this.Init(a,b,c)上得到了一个错误;
错误:对象不支持此属性或方法
发布于 2010-04-03 20:52:48
在声明myObject时忘记了new关键字
var myObject = new myclass(3, 5, 6);发布于 2010-04-04 03:53:16
出于好奇,有没有什么特别的原因让你有一个单独的"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或myAppname从外部访问;
https://stackoverflow.com/questions/2571278
复制相似问题