简体   繁体   English

jQuery或JavaScript是否具有类和对象的概念?

[英]Does jQuery or JavaScript have the concept of classes and objects?

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? ArticleVote是一个类并且submitVote()ArticleVote的一个函数吗?

Or what does the above code mean? 或者上面的代码是什么意思? And is there any concept of classes and objects in jQuery or in traditional JavaScript? 在jQuery或传统的JavaScript中是否有类和对象的概念? How to create them? 如何创建它们? Please share some reference links or code. 请分享一些参考链接或代码。

Everything is an object in JavaScript 一切都是JavaScript中的对象

As opposed to other purportedly pure OOP languages. 与其他据称纯粹的OOP语言相反。 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: 适当调用意味着您必须使用new运算符:

var obj = new ObjectCreator;

So while JavaScript does not have classes per se, has means to emulate that behavior. 因此,虽然JavaScript本身没有类,但有方法可以模拟该行为。 For example: 例如:

class Foo {
    public void bar() {}
}

Foo foo = new Foo();

is equivalent to the following JS code: 相当于以下JS代码:

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: 所以,给定两个伪类Foo和Bar,如果我们希望Bar从Foo扩展,我们必须写:

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. 所以JavaScript有对象文字,它们是某种哈希表,只是它们是自我意识的。 By self-conscious I mean that they know about the this keyword. 通过自我意识,我的意思是他们知道this关键字。 Object literals are a great way to implement the Singleton pattern. 对象文字是实现Singleton模式的好方法。

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 什么样的prototype东西

As many have said, in JavaScript objects inherit from objects. 正如许多人所说,JavaScript对象继承自对象。 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. 无论如何,这个原型概念与原型链的概念有关,它类似于经典OO语言中的父 - >子链。 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: 如果在foo对象上调用bar方法,但该对象没有bar方法,则启动成员查找阶段:

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. 经典的OO继承和基于原型的继承之间存在关键区别。 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. 我们可以说john是一个匿名人的寄生虫,因为它无情地吮吸了人的智慧。 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. 在经典的OO继承中,我们不能做到以上。 We could emulate it using static fields though. 我们可以使用静态字段来模拟它。 But that would make all instances of that class having the same value for that field. 但这会使该类的所有实例具有该字段的相同值。

Javascript支持对象但不支持 - 它使用基于原型的对象系统。

Yes, JavaScript has impressive support for Object Oriented programming, Objects and functions. 是的,JavaScript对面向对象的编程,对象和函数提供了令人印象深刻的支持。 In fact, I'm surprised that you have to ask this question! 事实上,我很惊讶你必须提出这个问题! There are a wealth of resources online such as: 网上有大量资源,例如:

http://mckoss.com/jscript/object.htm http://mckoss.com/jscript/object.htm

http://www.webreference.com/js/column79/ http://www.webreference.com/js/column79/

http://www.javascriptkit.com/javatutors/oopjs.shtml http://www.javascriptkit.com/javatutors/oopjs.shtml

More resources: http://www.google.com/search?q=object+oriented+programming+javascript 更多资源: http//www.google.com/search?q = object +oriented +programming +javascript

JavaScript frameworks such as jQuery and Prototype could not have been built without this support in JavaScript engines. 如果没有JavaScript引擎中的这种支持,jQuery和Prototype等JavaScript框架就无法构建。

Certainly JS has objects and classes, but it's not exactly a conventional approach. 当然JS有对象和类,但它不完全是传统的方法。

It's rather a broad question, but there's three main things you want to know about objects and classes in JS: 这是一个相当广泛的问题,但是您想要了解JS中的对象和类有三个主要内容:

1). 1)。 Everything is an object - this famously includes JS's first class functions 一切都是一个对象 - 这个着名的包括JS的第一类函数

2). 2)。 There are object literals 有对象文字

var myObject = { "foo": 123, "bar": [4,5,6] };

3). 3)。 Inheritance is prototype based, so creating classes is more a matter of form than function. 继承是基于原型的,因此创建类更多的是形式而非功能。 To get the effect of a class you'd write something like: 要获得课程的效果,你可以写下这样的内容:

function myClass(foo)
{
  this.foo = foo;
}
myClass.prototype.myFooMethod = function() {alert(this.foo);}

var myInstance = new myClass(123);

myinstance.myFooMethod(); // alerts 123

For your example it's likely that ArticleVote is an object instance and probably not conceptually a class, and submitVote would be a method of the object. 对于您的示例,ArticleVote可能是一个对象实例,可能在概念上不是一个类,submitVote可能是该对象的一个​​方法。 can't tell for sure though, it could be what you'd call a static method in another language. 虽然无法确定,但它可能是你用另一种语言称为静态方法的东西。

You can achieve the above through Javascript, nothing to do with jQuery. 你可以通过Javascript实现上述功能,与jQuery无关。

var ArticleVote= {}; 

ArticleVote.submitVote = function(voteResult) {
    console.log(voteResult);
}


function Vote(){
   ArticleVote.submitVote('no');
   return false;
}

You can use the JavaScript function as class. 您可以将JavaScript函数用作类。

ClassUtil = function(param){    
     privateFunction = function(param){    
        // Do some thing    
        return valueParam;
    }    
    this.publicFunction = function(){    
        var val1 = function1();    
        if (val1){    
            return true;    
        } else{    
            return false;
        }
    }
}

function getClass(){    
   var classUtil = new ClassUtil();     
   alert(classUtil.publicFunction());    
}

There is one public and one private function. 有一个公共和一个私人功能。 You can call public function from out side using object of the class. 您可以使用类的对象从外部调用公共函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM