六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

JavaScript的面向对象编程

[复制链接]

升级  26%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
13
 楼主| 发表于 2013-1-29 08:51:29 | 显示全部楼层 |阅读模式
 

面向对象编程(OOP),是目前主流的编程方式,似乎能够OOP的语言,才会被大多数人视为好语言,不能OOP的语言都是“奥特曼”。而JavaScript,则是常常被人误解成“奥特曼”的一种语言,殊不知,JavaScript有着一种更高级的OOP特性。
在传统的OOP语言中,Object是Class的一个实例,一个Class可以继承自另一个Class,我们可以理解为“基于类型(Class)”;而JavaScript的语法中并没有Class的概念,Object传承自哪里并不重要,重要的是它能做什么,我们可以理解为“基于原型(Prototype)”。下面就去看看JavaScript的OOP特性吧。
 
1、一切皆对象

在JavaScript中,一切都是对象(除了null、undefined),数字(Number)、字符串(String)、布尔值(Boolean)、函数(Function)、数组(Array)都是对象,都有属于自己的Method。不过要注意的是,Number、String、Boolean这几个基本类型对象是不可变的,即你无法添加、修改它们的方法、属性。
一个常见的误解是,Number的字面量(literal)并不是对象,因为无法直接调用它的方法(符号“.”会被解释为小数点),不过还是有很多方法可以让它看起来像一个Object:
<div style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-color: initial; vertical-align: baseline; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #2c2c29; font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; text-align: left;"><div class="syntaxhighlighter  js" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-color: initial; vertical-align: baseline; background-color: white !important; margin-top: 1em !important; margin-right: 0px !important; margin-bottom: 1em !important; margin-left: 0px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; width: 548px; font-size: 14px !important; line-height: 24px;">[table=98%,initial][tr][td] 
 
[/td][td]<div class="container" style="border-top-width: 0px !important; border-right-width: 0px !important; border-bottom-width: 0px !important; border-left-width: 0px !important; border-color: initial; vertical-align: baseline !important; background-image: none !important; background-color: initial !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; border-top-left-radius: 0px 0px !important; border-top-right-radius: 0px 0px !important; border-bottom-right-radius: 0px 0px !important; border-bottom-left-radius: 0px 0px !important; border-color: initial !important; float: none !important; height: auto !important; width: auto !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important;"><div class="line number1 index0 alt2" style="border-top-width: 0px !important; border-right-width: 0px !important; border-bottom-width: 0px !important; border-left-width: 0px !important; border-color: initial; vertical-align: baseline !important; background-image: none !important; background-color: white !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 1em !important; padding-bottom: 0px !important; padding-left: 1em !important; border-top-left-radius: 0px 0px !important; border-top-right-radius: 0px 0px !important; border-bottom-right-radius: 0px 0px !important; border-bottom-left-radius: 0px 0px !important; border-color: initial !important; float: none !important; height: auto !important; width: auto !important; font-size: 1em !important; white-space: pre-wrap !important;">// 2.toString();  直接调用出错:SyntaxError
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表