Home >Web Front-end >JS Tutorial >Discuss whether javascript is an object-oriented language_javascript skills

Discuss whether javascript is an object-oriented language_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:13:301251browse

1 It has objects that can contain data and methods for processing data. Objects can contain other objects. It does not have a class (before JavaScript 2.0 is actually implemented), but it has a constructor that can do what a class can do, including acting as a container and role for variables and methods. It doesn't have class-based inheritance, but it does have prototype-based inheritance. Two ways to build an object system are through inheritance and through aggregation. JavaScript has both.

2 Some comments say that JavaScript is not truly object-oriented because it cannot provide information hiding. That is, an object cannot have private variables or methods: all members are public. But then someone proved that JavaScript objects can have private variables and private methods. There are also criticisms that JavaScript cannot provide inheritance, but then someone proved that JavaScript can not only support traditional inheritance but also apply other code reuse patterns.

3 Saying that JavaScript is an object-based language is a correct and slightly conservative judgment, but saying that JavaScript is not object-oriented is a wrong perception in my opinion. In fact, there are sufficient reasons to prove it. JavaScript is an object-oriented language, but compared with traditional class-based object-oriented, javaScript has its own unique features. We call this uniqueness prototype-based object-oriented.

----------------------------------------

Because js is a weakly typed language. Unlike C, C#, this type of language! It does not support function method overloading. If you write a method before, and then write a method with the same name later, it will overwrite the previous method by default. !This situation is the same as in PHP!

It is not like C# and other languages ​​that support function overloading. Different methods are called according to different parameters and return values!

Regarding whether JS is object-oriented. It is not entirely object-oriented. It can also be written as classes, methods, and attributes. But it is slightly different from other languages! For example,
var cls={
my:{ a:0},
init:function()
{
alert(this.my.a);
}
};
window.onload=function()
{
cls.init();
}

Call cls.init();

This is his class, but it lacks many object-oriented features. It is very clear from the above! So it is not completely object-oriented

Object-oriented is just a way of thinking and a way of solving problems!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn