单例是在程序设计非常基础的东西,用React Native做了一个简单单例的例子,只是玩玩而已,也做了一个笔记。
单例的类,中间定了一个name的属性。
let instance = null;var name = '';export default class Singleton { constructor() { if(!instance){ instance = this; } return instance; } setName(name){ this.name=name; } getName(name){ return this.name; }}
设置name的属性,在其中一个Component里面,代码为20行21行。
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, Navigator, TouchableOpacity, View} from 'react-native';import LoginComponent from