OOP in Javascript(1)
来源:互联网 发布:python spark 编辑:程序博客网 时间:2024/05/16 09:18
- Basic ways of creating an object using javascript
- <script language=javascript type="text/javascript">
- <!--
- //create object using new object keyword
- //We define a custom object "person," then add to it its own properties and method afterwards. In this case, the custom method merely initializes two more properties.
- person=new Object();
- person.name="bennyxu";
- person.height=170;
- person.say =function()
- {
- this.state="running";
- window.alert("I'm running");
- }
- //create object using literal notation
- literalObj={
- property1: "hello",
- property2: "Bennyxu",
- property3: ["abc",2,3,4,"NEG"],
- method1: function()
- {
- alert("method had been called " + this.property1);
- }
- };
- var circle={x:0,
- y:0,
- radius:2} //another example
- //nesting is OK
- var rectangle ={
- upperLeft :{x:2,y:2},
- lowerRight:{x:4,y:4}
- }
- alert("nested definition:" + "uppperLeft=" + rectangle.upperLeft.x );
- function Button1_onclick() {
- person.say();
- }
- function Button2_onclick() {
- literalObj.method1();
- window.alert(literalObj.property3[2]);
- }
- //-->
- </script>
While using the new operator or literal notion to create a custom object is both
simple and logical, the biggest shortcoming is that the result is NOT reusable-
we cannot easily initialize different versions of the created object.
- OOP in Javascript(1)
- OOP in Javascript(2)
- OOP in Javascript(3)
- OOP in Javascript(4)
- How to OOP in Javascript
- JavaScript---OOP浅谈1
- JavaScript OOP
- JavaScript OOP
- JavaScript OOP
- JavaScript && OOP
- javascript OOP
- JavaScript OOP
- javascript OOP
- JavaScript OOP
- JavaScript OOP
- JavaScript OOP
- javaScript---oop
- OOP in JS
- 心存感激
- RMI-IIOP
- 编译安装apache2+php5+mysql5+ZendOptimizer+Cronolog简易文档
- !软件开发的无限乐趣——学习ssh框架心得
- [delphi] 直接操作Socket API写木马其实很简单(监听篇)
- OOP in Javascript(1)
- OOP in Javascript(2)
- OOP in Javascript(3)
- OOP in Javascript(4)
- Firefox 更新到 3.0.2 版本
- 数组中length属性属于Java中哪一个类
- namespace in javascript
- c#3.0新体验(一)
- C#3.0新体验 (二)