Javascript 原型对象的帮助
我正在学习javascript,通过prototype创建onject时遇到一些问题。Javascript 原型对象的帮助,javascript,prototype,Javascript,Prototype,我正在学习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) {
我有这个:
<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>
函数myclass(a、b、c){
if(arguments.length){this.Init(a,b,c);}
}
myclass.prototype.Init=函数(a、b、c){
此参数1=a;
这个参数2=b;
此参数3=c;
};
myclass.prototype.Print=函数(){
警报(this.param1+'-'+this.param2+'-'+this.param3);
};
var myObject=myclass(3,5,6);
myObject.Print();
但是我得到了一个错误,Init(a,b,c)错误:对象不支持此属性或方法您在声明
myObject
时忘记了new
关键字:
var myObject = new myclass(3, 5, 6);
只是出于好奇,您有一个单独的“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();
}());
“包装”是
这在这里基本上是毫无意义的,但这是你最终必须开始做的事情,所以最好现在就开始。这只是“包装”的一种方法,还有其他方法
基本上,按照脚本的编写方式,如果用户运行另一个脚本,该脚本包含一个名为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]从外部访问 这不起作用的原因是,当您使用“new”关键字时,构造函数中的
this
指向您刚刚创建的实例。但是,忘记new
会将this
关键字设置为window对象,因此调用window.Init;这很正确地导致对象不支持此属性或方法错误:)。由于这种危险,通常打算用作构造函数的函数以大写字母(My_类)开头命名,因此更容易发现缺少的新我可以从外部使用此包装器吗?比如:myExample.MyClass.print()?nemiss,我编辑了我的答案,以便根据您的问题进一步阐述。
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"
};