创建像库函数一样工作的函数 [英] creating functions to work like library functions

查看:60
本文介绍了创建像库函数一样工作的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能非常不清楚 - 所以让我解释一下。请原谅我

如果我搞砸了术语(让我知道正确的术语

):


我想要做的是创建一个库命名空间来封装

某些具有共同点的函数。我希望它以Math()对象的工作方式工作在
中 - 直接调用它,而不是

必须将它实例化为变量(对象?)


例如,使用Math对象,我可以直接打电话给

Math.floor() - 我不需要经历

的箍如下:


var math = new Math();

所以,让我说我有这个:


< script type =" text / javascript">

function MyLibrary(){


this.sayHi = function(){

alert(''嗨!'');

}


这个。 saySomething = function(something){

alert(something);

}

}

< / script> ;

为了使用''sayHi'',我必须实例化:


var library = new MyLibrary();

library.sayHi();

我想做的只是说:


MyLibrary.sayHi();

- >所以,我的问题是 - 有没有办法做到这一点?


链接也很感激 - 我会谷歌它,但我甚至不知道是什么

放入搜索框 - 我很确定我的术语已经过时了。


Thanx

解决方案



Tony写道:

这可能非常不清楚 - 所以让我解释一下。请原谅我
如果我搞砸了术语(让我知道正确的术语将是什么):

我想做的是创建一个库'' namespace''封装
某些共同点的函数。我想让它以Math()对象的工作方式工作 - 通过直接调用它,而不是必须将它实例化为变量(对象?)

例如,使用Math对象,我可以直接打电话给
Math.floor() - 我不需要经历如下的事情:

var math = new Math();

所以,让我说我有这个:

< script type =" text / javascript">
function MyLibrary(){

this.sayHi = function(){
alert(''嗨!'');
}

this.saySomething = function(something){
alert(something);
}
}
< / script>

为了使用' 'sayHi'',我必须实例化:

var library = new MyLibrary();
library.sayHi();

我想做什么只是说:

MyLibrary.sayHi();

- >所以,我的问题是 - 有没有办法做到这一点?

链接也很感激 - 我会谷歌它,但我甚至不知道放入什么
搜索框 - 我很确定我的术语已经过时了。

Thanx




你所描述的是所谓的静态对象。含义

你不必创建它的实例来使用它的属性

和方法。 Date和Math都是静态对象的例子。


使用你的例子,你可以这样做...


function MyLibrary (){//可以留空或声明私人成员}


MyLibrary.staticvariablehere = value;

MyLibrary.staticmethod = function(params){//这里的方法主体};


然后从那时起你可以使用MyLibrary.staticvariablehere和

MyLibrary.staticmethod(params)。


这是一个过于简化的版本,但应该适合你。以下链接

也简要介绍了它......

http://www.pasz.com/articles/JSInheritance.html


请记住,变量和方法定义了这个方式不要

可以直接访问构造函数中定义的私有成员。你

正在添加到类的模板,而不是实例。无论哪种方式,

这应该完全符合您的需要。希望它适合你。


Tony写道:

这可能非常不清楚 - 所以让我解释一下。请原谅我
如果我搞砸了术语(让我知道正确的术语将是什么):

我想做的是创建一个库'' namespace''封装
某些共同点的函数。我想让它以Math()对象的工作方式工作 - 通过直接调用它,而不是必须将它实例化为变量(对象?)

例如,使用Math对象,我可以直接打电话给
Math.floor() - 我不需要经历如下的事情:

var math = new Math();

所以,让我说我有这个:

< script type =" text / javascript">
function MyLibrary(){

this.sayHi = function(){
alert(''嗨!'');
}

this.saySomething = function(something){
alert(something);
}
}
< / script>

为了使用' 'sayHi'',我必须实例化:

var library = new MyLibrary();
library.sayHi();

我想做什么只是说:

MyLibrary.sayHi();

- >所以,我的问题是 - 有什么办法可以做到这一点吗?


是的。除了Martyr2建议的方式,您还可以使用:

var MyLibrary =(function()

{

var privateVar =''blah '';


函数privateMethod(){/*...*/}


//公共方法

返回{

sayHi:function(){

alert(''嗨'');

},

saySomething:function(something){

alert(something);

}

}

} )();

//调用公共方法

MyLibrary.sayHi();

MyLibrary.saySomething('''blah blah''' );

您还可以拥有特权方法,这样您就可以更改

privateVar的值,否则外部函数将无法访问这些值

MyLibrary 。


通常使用大写字母表示构造函数的名称,

以及库名称应该保持简短,因为它们的前缀是你的所有/>
方法调用。


另一种方式是创建一个对象并添加东西到它的原型,

但是如果你不打算用它作为构造函数那么

加到原型之间的区别或直接对象可能

没有兴趣。


链接也很感激 - 我会谷歌,但我甚至不知道知道放在搜索框中的内容 - 我很确定我的术语已经过时了。




试试这个:


< URL:http://javascript.crockford.com/private.html>

-

Rob


Martyr2写道:

Tony写道:
< stuff>
你所描述的是所谓的静态对象。含义
您不必创建它的实例来使用其属性
和方法。 Date和Math都是静态对象的示例。


啊 - thanx用于澄清。

使用你的例子,你可以这样做...

功能MyLibrary(){//可以留空或声明私人成员}

MyLibrary.staticvariablehere = value;
MyLibrary.staticmethod = function(params){//此处方法的主体};

然后从那时起你可以使用MyLibrary.staticvariablehere和
MyLibrary.staticmethod(params)。


嗯 - 我试过了,但它没有用。也许我做错了什么 -

我会再给它一次。

这是一个过于简化的版本但应该适合你。以下链接简要介绍了它......

http://www.pasz.com/articles/JSInheritance.html




感谢您的链接 - 这将有助于我想,有点。

特别是现在我知道该怎么称呼我正在寻找的东西:)


And that is probably very unclear - so let me explain. Please forgive me
if I screw up the terminology (and let me know what the correct term
would be):

What I want to do is to create a library ''namespace'' to encapsulate
certain functions that have something in common. I want it to work in
the way the Math() object works - by calling it directly, instead of
having to instantiate it as a variable (object?)

For example, with the Math object, I can just make a call to
Math.floor() directly - I don''t have to go through the hoops of
delcaring something like:

var math = new Math();
So, lets say I have this:

<script type="text/javascript">
function MyLibrary() {

this.sayHi = function() {
alert(''Hi!'');
}

this.saySomething = function(something) {
alert(something);
}
}
</script>
In order to use ''sayHi'', I have to instantiate this:

var library = new MyLibrary();
library.sayHi();
What I WANT to do is just say:

MyLibrary.sayHi();

-> So, my question is - is there any way to do this?

Links are also appreciated - I''d google it, but I don''t even know what
to put in the search box - I''m pretty sure my terminology is way off.

Thanx

解决方案


Tony wrote:

And that is probably very unclear - so let me explain. Please forgive me
if I screw up the terminology (and let me know what the correct term
would be):

What I want to do is to create a library ''namespace'' to encapsulate
certain functions that have something in common. I want it to work in
the way the Math() object works - by calling it directly, instead of
having to instantiate it as a variable (object?)

For example, with the Math object, I can just make a call to
Math.floor() directly - I don''t have to go through the hoops of
delcaring something like:

var math = new Math();
So, lets say I have this:

<script type="text/javascript">
function MyLibrary() {

this.sayHi = function() {
alert(''Hi!'');
}

this.saySomething = function(something) {
alert(something);
}
}
</script>
In order to use ''sayHi'', I have to instantiate this:

var library = new MyLibrary();
library.sayHi();
What I WANT to do is just say:

MyLibrary.sayHi();

-> So, my question is - is there any way to do this?

Links are also appreciated - I''d google it, but I don''t even know what
to put in the search box - I''m pretty sure my terminology is way off.

Thanx



What you are describing is what is called a "static object". Meaning
that you don''t have to create an instance of it to use its properties
and methods. Both Date and Math are examples of static objects.

Using your example, here is how you would do it...

function MyLibrary() { //Can leave empty or declare private members }

MyLibrary.staticvariablehere = value;
MyLibrary.staticmethod = function (params) { //body of method here };

Then from that point on you can use MyLibrary.staticvariablehere and
MyLibrary.staticmethod(params).

This is an overly simplified version but should work for you. The link
below briefly talks about it also...

http://www.pasz.com/articles/JSInheritance.html

Just keep in mind that variables and methods defined this way do not
have direct access to private members defined in the constructor. You
are adding to the template of the class, not instances. Either way,
this should do exactly what you need. Hope it works for you.


Tony wrote:

And that is probably very unclear - so let me explain. Please forgive me
if I screw up the terminology (and let me know what the correct term
would be):

What I want to do is to create a library ''namespace'' to encapsulate
certain functions that have something in common. I want it to work in
the way the Math() object works - by calling it directly, instead of
having to instantiate it as a variable (object?)

For example, with the Math object, I can just make a call to
Math.floor() directly - I don''t have to go through the hoops of
delcaring something like:

var math = new Math();
So, lets say I have this:

<script type="text/javascript">
function MyLibrary() {

this.sayHi = function() {
alert(''Hi!'');
}

this.saySomething = function(something) {
alert(something);
}
}
</script>
In order to use ''sayHi'', I have to instantiate this:

var library = new MyLibrary();
library.sayHi();
What I WANT to do is just say:

MyLibrary.sayHi();

-> So, my question is - is there any way to do this?
Yes. In addition to the way suggested by Martyr2 you can use:
var MyLibrary = (function()
{
var privateVar = ''blah'';

function privateMethod(){ /*...*/ }

// Public methods
return {
sayHi : function() {
alert(''Hi'');
},
saySomething : function(something) {
alert(something);
}
}
})();
// Call the public methods
MyLibrary.sayHi();
MyLibrary.saySomething(''blah blah'');
You can also have privileged methods so you can change the value of
privateVar, which would otherwise be inaccessible to functions outside
MyLibrary.

It is convention to use a capital letter for the names of constructors,
and also library names should be kept short since they prefix all your
method calls.

Another way is to create an object and add stuff to its prototype,
though if you aren''t going to use it as a constructor then the
difference between adding to the prototype or directly to the object may
be of no interest.

Links are also appreciated - I''d google it, but I don''t even know what
to put in the search box - I''m pretty sure my terminology is way off.



Try this:

<URL:http://javascript.crockford.com/private.html>
--
Rob


Martyr2 wrote:

Tony wrote: <stuff>
What you are describing is what is called a "static object". Meaning
that you don''t have to create an instance of it to use its properties
and methods. Both Date and Math are examples of static objects.
Ah - thanx for the clarification.
Using your example, here is how you would do it...

function MyLibrary() { //Can leave empty or declare private members }

MyLibrary.staticvariablehere = value;
MyLibrary.staticmethod = function (params) { //body of method here };

Then from that point on you can use MyLibrary.staticvariablehere and
MyLibrary.staticmethod(params).
Hmm - I tried that but it didn''t work. Maybe I did something wrong -
I''ll give it another go.

This is an overly simplified version but should work for you. The link
below briefly talks about it also...

http://www.pasz.com/articles/JSInheritance.html



And thanks for the link - that will help quite a bit, I think.
Especially now that I know what to call what I''m looking for :)


这篇关于创建像库函数一样工作的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆