20170810

js

所有的引用類型(數(shù)組、對(duì)象、函數(shù)),都具有對(duì)象特性,即可自由擴(kuò)展屬性(除了“null”意外)
var obj = {};
obj.a = 100;
var arr = [];
arr.a = 100;
function fn() {}
fn.a = 100;
所有的引用類型(數(shù)組、對(duì)象、函數(shù)),都有一個(gè)proto(隱式原型)屬性,屬性值是一個(gè)普通的對(duì)象
console.log(obj.__proto__);//{__defineGetter__: ?, __defineSetter__: ?, hasOwnProperty: ?, __lookupGetter__: ?, __lookupSetter__: ?, …}
console.log(arr.__proto__);//[constructor: ?, toString: ?, toLocaleString: ?, join: ?, pop: ?, …]
console.log(fn.__proto__);//? () { [native code] }
所有的函數(shù),都有一個(gè)prototype(顯式原型)屬性,屬性值也是一個(gè)普通對(duì)象
console.log(fn.prototype);//{constructor: ?}
所以的引用類型(數(shù)組、對(duì)象、函數(shù)),proto屬性值指向它的構(gòu)造函數(shù)的“prototype”屬性值
console.log(obj.__proto__===Object.prototype)//true
當(dāng)試圖得到一個(gè)對(duì)象的某個(gè)屬性時(shí),如果這個(gè)對(duì)象本身沒有這個(gè)屬性,那么會(huì)去它的proto(即它的構(gòu)造函數(shù)的prototype)中尋找
function Foo(name, age){
  this.name = name
}

Foo.prototype.alertName = function(){
  alert(this.name)
}

var f = new Foo('zhangsan');
f.printName = function(){
  console.log(this.name);
}
f.printName()
f.alertName()

rn

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

export default class MyApp extends Component {
  constructor(props){
    super(props)
    this.state = {times:0}
  }
  timePlus() {
    let times = this.state.times

    times++;
    this.setState({
      times: times
    })
  }

  componentWillMount() {
    console.log('componentWillMount');
  }
  componentDidMount() {
      console.log('componentDidMount');
  }
  shouldComponentUpdate() {
      console.log('shouldComponentUpdate');
      return true;
  }
  componentWillUpdate() {
      console.log('componentWillUpdate');
  }
  componentDidUpdate() {
      console.log('componentDidUpdate');
  }

  render() {
    console.log('render');
    return (
      <View style={styles.container}>
        <Text style={styles.welcome} onPress={this.timePlus.bind(this)}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
            {this.state.times}次
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('MyApp', () => MyApp);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容