JavaScript - 对象


1. 2 ways to create Object in JavaScript

  1. Literal notation is the one where we declare the object, and, at the same time, define properties and values in the object. It makes use of { }. See first one for an example.

    //Literal
    var australia = {
    weather: "superb",
    people: "not many of them but they're all great!",
    tourism: "a wonderful place to holiday. please visit!"};
  2. Constructor notation is where we make use of the keywords new Object(). We then use dot notation to add property and values to the object. See second one for an example.

    //Constructor
    var jordan = new Object();
    jordan.weather = "hot. but so are the people!";
    jordan.people = "see above!";
    jordan.tourism = "Codecademy's dream team retreat!";


2. There are two different ways that we can retrieve an object's properties: dot and bracket notation.

  1. Dot notation: obj.myProp
  2. Bracket notation: obj["myProp"] (You can also use variable in bracket notation.


3. Functions in object are called Method.


4. this keyword: Why is using the this keyword helpful? Imagine we have a method that has a lot of complicated code and references an object's properties. We also want many objects to have this method. We can create a single generic method that uses the this keyword. Now many objects can use this method by defining a method that simply equals this generic method.

使用this,就可以不指定特定对象名称。任何一个调用该对象的实例,都可以使用这个属性或方法。

this也只能用在对象中!
所以当你在对象以外调用时,需要用objectName.functionName,在对象内,则可使用this.functionName。

优质内容筛选与推荐>>
1、Ubuntu:开启samba服务未能正常打开linux共享文件的解决办法
2、livehttpheaders
3、[导入]工行力推网银业务
4、git stash和git stash pop
5、linux系统下安装单台Redis


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号