JavaScript本身并不是完全的面向对象的语言,它和C++/Java的相似程度还没有和Lisp等函数式编程语言来得接近。 有人说,JavaScript是披着面向对象的语法糖衣的函数式编程语言。
想要学好JavaScript, 对它的某些特性就不得不有所了解:
1. 灵活性
JavaScript提供了许多灵活的方式来实现某个特定的功能,你可以用简单的,面向进程的方式来实现,你也可以用很复杂的,面向对象的方法。
举个例子,如果你曾经是一个C开发人员:
/* Start and stop animations using functions. */
function startAnimation() {
...
}
function stopAnimation() {
...
}
又或者你是个Java程序员:
/* Anim class. */
var Anim = function() {
...
};
Anim.prototype.start = function() {
...
};
Anim.prototype.stop = function() {
...
};
/* Usage. */
var myAnim = new Anim();
myAnim.start();
...
myAnim.stop();
2. 弱类型语言
和C++/Java不同,JavaScript是弱类型语言,这意味着你可以为某个声明的变量赋任意值。
JavaScript有两种数据类型: 基本数据类型(number,boolean和string), 引用数据类型(包括对象,数组已经方法)。当然还有null以及undefined。虽然对于数据类型的分类跟C++/Java有些不同,但事实上在使用中,你并不会感觉到太多这种差异。同时,对于传参方式,传值或是传址,JavaScript同C++/Java并无二致。
3. 函数是一类对象(functions as first-class objects)
对于这句话我也不知道该如何翻译,于是把英文原文贴过来。许多面向对象语言宣称“万事万物皆对象”,但事实上方法/函数并没有被归类于对象。而在JavaScript中,函数同对象是同一等级的事物。
如同我们可以创建匿名类一样,我们也可以创建匿名函数:
(function() {
var foo = 10;
var bar = 2;
alert(foo * bar);
})();
4. 对象的可变性
你可以为一个函数赋予一个属性:
function displayError(message) {
displayError.numTimesExecuted++;
alert(message);
};
displayError.numTimesExecuted = 0;
5. 高阶函数
一个高阶函数可以将函数作为参数,也可以返回一个函数。此特性让 JavaScript 程序员可以用 Java 语言所不能提供的方法来操纵函数。
hot = function hot() {
alert('Hot.')
}
cold = function cold() {
alert('Cold.')
}
var current;
function swap(hot, cold) {
if(current == hot) {
current = cold
} else {
current = hot
}
return current;
}
6.对象模型
JavaScript 用嵌套函数表示对象, 它基于原型或现有的对象的实例来构造对象,而非基于类模板。
Animal = function() {
this.name = "nobody"
this.speak = function () {
return "Who am I?"
}
}
Dog = function() {
this.speak = function() {
return "Woof!"
}
}
Dog.prototype = new Animal();
弄清楚以上的这些概念,可以说对JavaScript就有了一定的了解了。
分享到:
相关推荐
With Learning JavaScript Design Patterns, you’ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to...
it is the only language that can be used to write the entire stack, including the front-end, mobile (native and hybrid) platforms, and back-end. It is crucial for JavaScript developers to understand ...
Chapter 1, Welcome to the Node.js Platform, serves as an introduction to the world of Node.js application design by showing the patterns at the core of the platform itself. It covers the Node.js ...
and other language-specific categories, the abstractions and code templates in this guide are ideal -- whether you're writing a client-side, server-side, or desktop application with JavaScript....
Additionally, considering the potential need for system maintenance, the B/S (Browser/Server) design pattern was utilized, allowing server-side updates without requiring any changes on the client-...
The frontend development involves the use of essential web technologies such as Cascading Style Sheets (CSS) for styling, Hypertext Markup Language (HTML5) for structure, and JavaScript (JS) for ...
**Chapter 1: A New Design for the Web** - **Description**: This chapter introduces the fundamental principles of Ajax from both a usability and technological perspective. It explores how Ajax can ...
you'll learn how by combining the ASP.NET MVC server-side language, the Bootstrap front-end framework, and Knockout.js - the JavaScript implementation of the Model-View-ViewModel pattern. Author ...
In this project, the event extraction information management system is implemented using JavaScript for the frontend, while the backend design primarily revolves around the Python language....
The page design follows the Model-View-Controller (MVC) framework, while the backend employs JavaScript along with other scripting languages. This project integrates knowledge from software ...
In this specific project, the use of Java as the primary programming language ensures the scalability, robustness, and compatibility of the system. It allows developers to create reusable code and ...
8. **前后端分离(Frontend-Backend Separation)**:An architectural pattern where the presentation layer (front-end) and the data processing layer (back-end) operate independently, improving ...
Another way of thinking about Action class is as the Adapter design pattern. The purpose of the Action is to "Convert the interface of a class into another interface the clients expect. Adapter lets ...
you’ll learn how by combining the ASP.NET MVC server-side language, the Bootstrap front-end framework, and Knockout.js—the JavaScript implementation of the Model-View-ViewModel pattern. Author ...
It then shows these concepts at work through the Factory Design Pattern. Chapter 4, Decorators, Generics, and Asynchronous Features, discusses the more advanced language features of decorators and ...
【标题】:“0-1-knapsack-problem-master (15).zip”这个压缩包文件的名字似乎与实际内容不符,因为标题提到的是一个优化问题——0-1背包问题,而描述却提到了“html登录注册页面”。这可能是命名上的误解。我们将...
在"design-pattern-master"这个压缩包中,可能包含了关于设计模式的各种实现示例,可能包括各个设计模式的源代码、解释文档或者教程。通过学习和理解这些例子,开发者可以更好地将设计模式应用到实际项目中,提升...