jQuery或JavaScript有类和对象的概念吗? [英] Does jQuery or JavaScript have the concept of classes and objects?

查看:85
本文介绍了jQuery或JavaScript有类和对象的概念吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某处找到了以下代码,但我无法正确理解代码。

  ArticleVote.submitVote('no'); return false; 

ArticleVote 一个类和 submitVote()该类的函数?



或者上面的代码是什么意思?并且在jQuery或传统的JavaScript中有类和对象的任何概念吗?如何创建它们?请共享一些参考链接或代码。

解决方案

一切都是JavaScript中的对象



与其他纯粹的纯OOP语言相反。函数也是对象,但它们也可以是对象的构造函数。

  var ObjectCreator = function(){
};

上面是一个函数,如果适当调用,创建一个对象。 var obj = new ObjectCreator;

因此,尽管JavaScript本身没有类,但是具有模拟该行为的手段。例如:

  class Foo {
public void bar(){}
}

Foo foo = new Foo();

等效于以下JS代码:

  var Foo = function(){
// constructor
};

Foo.prototype.bar = function(){}

var foo = new Foo;



继承是不同的



当你想使用继承,这是一个不同类型的继承(原型)。所以,给定两个伪类Foo和Bar,如果我们想要Bar从Foo扩展,我们必须写:

  var Foo = function(){}; 
var Bar = function(){};

Bar.prototype = new Foo; //这是继承阶段

var bar = new Bar;

警报(bar instanceof Foo);



对象字面值



有用,有时我们只需要只有一个该对象的实例。编写一个构造函数,然后用属性和方法填充它的原型是一种繁琐。所以JavaScript有对象字面量,这是一些哈希表,只是他们是自我意识。由自我意识我的意思是他们知道这个关键字。对象字面量是实现Singleton模式的好方法。

  var john = {
age:24,

isAdult:function(){
return this.age> 17;
}
};

以上,使用构造函数将等价于以下内容:

  var Person = function(age){
this.age = age;
};

Person.prototype.isAdult = function(){
return this.age> 17;
};

var john = new Person(24);



原型 >

正如许多人所说,在JavaScript对象从对象继承。这个东西有有用的方面,其中一个可能被称为寄生继承(如果我记得正确的道格拉斯·克罗克福德提到的上下文)。无论如何,这个原型概念与原型链的概念相关联,其类似于古典OO语言中的父 - >子链。所以,继承的东西。如果在 foo 对象上调用 bar 方法,但该对象没有 bar 方法,成员查找阶段开始:

  var Baz = function(){}; 

Baz.prototype.bar = function(){
alert(1);
};

var Foo = function(){};
Foo.prototype = new Baz;

var foo = new Foo;

/ *
* foo.bar是否存在?
* - 是的。然后执行它
* - no
*构造函数的原型对象是否有一个bar
*属性?
* - 是的。然后执行它
* - no
*是否有一个构造函数的原型对象
*初始构造函数? (在我们的例子中是Baz)
* - 是的。然后它必须有一个原型。在原型对象中查找一个bar
*成员。
* - no。好的,我们放弃了。抛出错误。
* /
foo.bar();



继续,你说了一些关于寄生继承的事。



在古典OO继承和基于原型的继承之间有一个关键的区别。当对象从对象继承时,它们还会继承状态。以这个例子:

  var Person = function(smart){
this.smart = smart;
};

var Adult = function(age){
this.age = age;
};

Adult.prototype = new Person(true);

var john = new Adult(24);

alert(john.smart);

我们可以说 john 的匿名人,因为它无情吸人的智力。此外,鉴于上述定义,所有未来的成年人都将是聪明的,不幸的是并不总是这样。但这并不意味着对象继承是一件坏事。只是一个工具,像任何东西。我们必须使用它,因为我们认为合适。



在经典的OO继承,我们不能做上述。我们可以使用静态字段来模拟它。但是,这将使该类的所有实例具有相同的值字段。


I found the following code somewhere, but I am not understanding the code properly.

ArticleVote.submitVote('no');return false;

Is ArticleVote a class and submitVote() a function of that class?

Or what does the above code mean? And is there any concept of classes and objects in jQuery or in traditional JavaScript? How to create them? Please share some reference links or code.

解决方案

Everything is an object in JavaScript

As opposed to other purportedly pure OOP languages. Functions are objects too, but they may just as well be constructor of objects.

var ObjectCreator = function () {
};

The above is a function which if called appropriately, creates an object. Called appropriately means that you have to use the new operator:

var obj = new ObjectCreator;

So while JavaScript does not have classes per se, has means to emulate that behavior. For example:

class Foo {
    public void bar() {}
}

Foo foo = new Foo();

is equivalent to the following JS code:

var Foo = function () {
    // constructor
};

Foo.prototype.bar = function () {}

var foo = new Foo;

Inheritance is different

The real difference comes when you want to use inheritance, which is a different type of inheritance (prototypal). So, given two pseudo-classes Foo and Bar, if we want Bar to extend from Foo, we would have to write:

var Foo = function () {};
var Bar = function () {};

Bar.prototype = new Foo; // this is the inheritance phase

var bar = new Bar;

alert(bar instanceof Foo);

Object literals

While constructor functions are useful, there are times when we only need only one instance of that object. Writing a constructor function and then populate its prototype with properties and methods is somehow tedious. So JavaScript has object literals, which are some kind of hash tables, only that they're self-conscious. By self-conscious I mean that they know about the this keyword. Object literals are a great way to implement the Singleton pattern.

var john = {
    age : 24,

    isAdult : function () {
        return this.age > 17;
    }
};

The above, using a constructor function would be equivalent to the following:

var Person = function (age) {
    this.age = age;
};

Person.prototype.isAdult = function () {
    return this.age > 17;
};

var john = new Person(24);

What about that prototype thingy

As many have said, in JavaScript objects inherit from objects. This thing has useful aspects, one of which may be called, parasitic inheritance (if I remember correctly the context in which Douglas Crockford mentioned this). Anyway, this prototype concept is associated with the concept of prototype chain which is similar to the parent -> child chain in classical OO languages. So, the inheritance stuff. If a bar method is called on a foo object, but that object does not have a bar method, a member lookup phase is started:

var Baz = function () {};

Baz.prototype.bar = function () {
    alert(1);
};

var Foo = function () {};
Foo.prototype = new Baz;

var foo = new Foo;

/*
 * Does foo.bar exist?
 *      - yes. Then execute it
 *      - no
 *          Does the prototype object of the constructor function have a bar
 *          property?
 *              - yes. Then execute it
 *              - no
 *                  Is there a constructor function for the prototype object of
 *                  the initial construct function? (in our case this is Baz)
 *                      - yes. Then it must have a prototype. Lookup a bar
 *                        member in that prototype object.
 *                      - no. OK, we're giving up. Throw an error.
 */
foo.bar();

Hold on, you said something about parasitic inheritance

There is a key difference between classical OO inheritance and prototype-based inheritance. When objects inherit from objects, they also inherit state. Take this example:

var Person = function (smart) {
    this.smart = smart;
};

var Adult = function (age) {
    this.age = age;
};

Adult.prototype = new Person(true);

var john = new Adult(24);

alert(john.smart);

We could say that john is a parasite of an anonymous Person, because it merciless sucks the person intelligence. Also, given the above definition, all future adults will be smart, which unfortunately is not always true. But that doesn't mean object inheritance is a bad thing. Is just a tool, like anything else. We must use it as we see fit.

In classical OO inheritance we can't do the above. We could emulate it using static fields though. But that would make all instances of that class having the same value for that field.

这篇关于jQuery或JavaScript有类和对象的概念吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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