我知道之前已经回答了这个问题,但我仍然感到困惑(这并不完全是我的错,因为我注意到答案可能完全不同).
我来自Java背景,所以如果你可以定义像静态,私有,公共等那样的东西,这应该有助于我理解.
基本上我想做一个完全自定义的类,但我不确定原型/等.示例(使用一种函数类型):
function myClass()
{
var a;
var b;
var helper = function()
{
this.a += this.b;
}
var helper2 = function(a,b)
{
return(a + b);
}
var getA = function()
{
return(this.a);
{
var staticMethodThatNeedsToBePublic = function()
{}
}
var myInstance = new myClass();
myClass.prototype.example1 = function(){};
myClass.example2 = function(){};
Run Code Online (Sandbox Code Playgroud)
那么这应该怎么写呢?(我试图包括所有基本的函数类型,但是如果我错过了任何随意添加的话)[注意:我并不特别关心这个具体的例子,我只是觉得这对谈话有帮助但是随意回答我的一般问题]
Mar*_*ahn 13
简短回答你的问题:使用原型.始终使用原型.
主要区别在于,如果使用this.foo = function(){}函数附加函数,则会为该类的每个实例重新声明该函数.使用附加func.prototype.foo = function(){}装置的功能只是不断被宣布一次,this它调用它应该是指的类的实例时性能得到改变.
您的代码变为:
function myClass(){
// constructor
}
myClass.prototype = new superClass();
myClass.constructor = myClass;
myClass.prototype = {
helper : function(){},
helper2 : function(){}
};
var myInstance = new myClass();
Run Code Online (Sandbox Code Playgroud)
从我5年前撰写的文章中向类添加方法和属性的完整列表:
http://www.htmlgoodies.com/primers/jsp/article.php/3600451/Javascript-Basics-Part-8.htm
function Cat(name, color){
/*
Constructor: any code in here is run when the object is created
*/
Cat.cats++;
/*
Private variables and functions - may only be accessed by private or privileged functions.
Note that 'name' and 'color', passed into the Class, are already private variables.
*/
var age = 0;
var legs = 4;
function growOlder(){
age++;
}
/*
Public variables - may be accessed publicly or privately
*/
this.weight = 1;
this.length = 5;
/*
Privileged functions - may be accessed publicly or privately
May access Private variables.
Can NOT be changed, only replaced with public versions
*/
this.age = function(){
if(age==0) this.length+=20;
growOlder();
this.weight++;
}
}
/*
Prototyped Functions - may be accessed publicly
*/
Cat.prototype = {
talk: function(){ alert('Meow!'); },
callOver: function(){ alert(this.name+' ignores you'); },
pet: function(){ alert('Pet!'); }
}
/*
Prototyped Variables - may be accessed publicly.
May not be overridden, only replaced with a public version
*/
Cat.prototype.species = 'Cat';
/*
Static variables and functions - may be accessed publicly
*/
Cat.cats = 0;
Run Code Online (Sandbox Code Playgroud)
夏季:
function MyClass(){
//You can access everything from in here (and from all sub functions) including prototypes and statics that are defined afterwards.
var privateVariable = "PriTest"; //Pair cannot by seen outside of MyClass
function privateFunction(){
}
this.publicVariable = "pubTest"; //Pair can be seen by everything except static functions
function publiFunction(){
}this.publiFunction = publiFunction;
this.prototypeFunction(); //This group could of been called like this from anywhere in this object
alert(this.prototypeVariable);
MyClass.staticFunction();
alert(MyClass.staticVariable);
}
MyClass.prototype.prototypeFunction = function(){
//Can access other prototypes, statics, and public functions and variables
this.publicFunction();
this.prototypeVariable;
MyClass.staticFunction();
}
MyClass.prototype.prototypeVariable = "proTest"
MyClass.staticFunction = function(){
//Can access other statics only
alert(MyClass.staticVariable);
}
MyClass.staticVariable = "staTest"
Run Code Online (Sandbox Code Playgroud)
如果我下面的内容有什么问题,请告诉我。
私有(内部可访问):[与java相同]
var variableName|| function functionName物体内部。只能由其他私有或特权函数访问。
公共和特权(外部可访问{仍然可以与所有内部对象一起工作}):[与java的公共相同]
this.variableName|| this.functionName = function(){ ... }物体内部。
原型(被其他原型访问):[几乎在类之外,只能访问公开可用的对象]
Class.prototype.variableName|| Class.prototype.functionName
以这种方式声明的函数将可以访问任何公共变量或原型变量。尝试更改以这种方式创建的变量将在对象上创建一个新的公共变量,并且原型变量将不可用。
静态:[与java相同?]
Class.variableName|| Class.functionName
可以通过任何函数或方法进行更改。
function MyClass(){
//public, privileged, and private
//Everything in here can see each other
//Everything in here can see everything outside
}
//Prototype and Static
//Prototype, Basically a template that is used on an instance of a class (And therefore does not have access to any of the non public fields)
Run Code Online (Sandbox Code Playgroud)
原型示例:
MyClass.prototype.proExample = function(){
this.doSomething;
}
//Is basically equivalent to
function proExample(instanceOfMyClass){
instanceOfMyClass.doSoemthing;
}
Run Code Online (Sandbox Code Playgroud)
我做一些测试后会添加到此。